Home>Article>Backend Development> Detailed explanation of usage examples of PHP namespace and automatic loading classes

Detailed explanation of usage examples of PHP namespace and automatic loading classes

伊谢尔伦
伊谢尔伦 Original
2017-06-30 09:48:10 1132browse

This article mainly introducesPHP object-orientednamespace andautomatic loading classof programming, and analyzesphp namespaceand automatic loading in combination with examples. The concept, function, usage and relatedNotesof loading classes, friends in need can refer to the following

This article describes the namespace and automatic loading classes of PHP object-oriented programming with examples. Share it with everyone for your reference, the details are as follows:

Namespace

Avoid duplication of class names and cause errors.

name = $name; } public function getName() { return $this->name; } } $obj = new Outputter(); // 同一命名空间下,类名不能相同,默认命名空间为空。空也是一种命名空间。 $obj -> setName("Jack"); print $obj->getName(); //namespace useful; // 更改命名空间,否则查询不到Hello类,Fatal error: Class 'my\Hello' not found $hello = new Hello(); ?> 

How to call classes in the namespace

Use the use keyword

Use the following Processing, you can directly call the class

\ to represent the global

global.php

 

Output:

hello from com\ getinstance\util
hello from global

namespace plus {}

output:

hello from Debug

Global namespace

autoload automatic loading class

ShopProduct.php

 

output:

ShopProduct constructor

Further optimization processing

is located in the folder business/ShopProduct.php

 

output:

ShopProduct constructor
business_ShopProduct constructor

The above is the detailed content of Detailed explanation of usage examples of PHP namespace and automatic loading classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn