


The examples in this article describe the namespace and automatic loading classes of PHP object-oriented programming. 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 a class in a 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 to 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(); ?>
\represents 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 Automatically load 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 constructorFurther 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

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools