


Detailed explanation of usage examples of PHP namespace and automatic loading classes
This article mainly introduces PHP object-orientednamespace and automatic loading class of programming, and analyzes php namespace and automatic loading in combination with examples. The concept, function, usage and related Notes of 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.
<?php require_once "useful/Outputter.php"; class Outputter { // output data private $name; public function setName($name) { $this->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(); ?> <?php // useful/Outputter.php namespace useful; // 命名空间 class Outputter { // } class Hello { } ?>
How to call classes in the namespace
<?php namespace com\getinstance\util; class Debug { static function helloWorld() { print "hello from Debug\n"; } } namespace main; // com\getinstance\util\Debug::helloWorld(); // 找不到Debug类 \com\getinstance\util\Debug::helloWorld(); // 加斜杠之后,就从根部去寻找了。 // outPut:hello from Debug ?>
Use the use keyword
<?php namespace com\getinstance\util; class Debug { static function helloWorld() { print "hello from Debug\n"; } } namespace main; use com\getinstance\util; //Debug::helloWorld(); //Fatal error: Class 'main\Debug' not found util\Debug::helloWorld(); ?>
Use the following Processing, you can directly call the class
<?php namespace com\getinstance\util; class Debug { static function helloWorld() { print "hello from Debug\n"; } } namespace main; use com\getinstance\util\Debug; // 直接使用到类 Debug::helloWorld(); ?>
\ to represent the global
global.php
<?php // no namespace class Lister { public static function helloWorld() { print "hello from global\n"; } } ?> <?php namespace com\getinstance\util; require_once 'global.php'; class Lister { public static function helloWorld() { print "hello from ".NAMESPACE."\n"; // NAMESPACE当前namespace } } Lister::helloWorld(); // access local \Lister::helloWorld(); // access global ?>
Output:
hello from com\ getinstance\util
hello from global
namespace plus {}
<?php namespace com\getinstance\util { class Debug { static function helloWorld() { print "hello from Debug\n"; } } } namespace main { \com\getinstance\util\Debug::helloWorld(); } ?>
output:
hello from Debug
Global namespace
<?php namespace { // 全局空间 class Lister { public static function helloWorld() { print "hello from global\n"; } } } namespace com\getinstance\util { class Lister { public static function helloWorld() { print "hello from ".NAMESPACE."\n"; } } Lister::helloWorld(); // access local \Lister::helloWorld(); // access global } ?>
autoload automatic loading class
ShopProduct.php
<?php class ShopProduct { function construct() { print "ShopProduct constructor\n"; } } ?> <?php function autoload( $classname ) { // 自动加载,根据类名加载类 include_once( "$classname.php" ); } $product = new ShopProduct( 'The Darkening', 'Harry', 'Hunter', 12.99 ); ?>
output:
ShopProduct constructor
Further optimization processing
is located in the folder business/ShopProduct.php
<?php class business_ShopProduct { // 这里的类命名就要遵循规则了 function construct() { print "business_ShopProduct constructor\n"; } } ?> <?php function autoload( $classname ) { $path = str_replace('_', DIRECTORY_SEPARATOR, $classname ); // 智能化处理 require_once( "$path.php" ); } $x = new ShopProduct(); $y = new business_ShopProduct(); ?>
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!

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
