PHP Object-Oriented Programming namespace and automatic loading class details, python object-oriented explanation
This article describes the namespace and automatic loading class of PHP object-oriented programming. We will share this with you for your reference. The details are as follows:
Namespace
Avoid duplicate class names and errors.
<? Phprequire_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 (); // The class names cannot be the same in the same namespace. The default namespace is null. Null is also a namespace. $ Obj-> setName ("Jack"); print $ obj-> getName (); // namespace useful; // change the namespace. Otherwise, the Hello class cannot be found. Fatal error: class 'my \ hello' not found $ Hello = new hello ();?> <? Php // useful/Outputter. phpnamespace useful; // namespace class Outputter {//} class Hello {}?>
How to call a class in a namespace
<? Phpnamespace com \ getinstance \ util; class Debug {static function helloWorld () {print "hello from Debug \ n" ;}} namespace main; // com \ getinstance \ util \ Debug:: helloWorld (); // The Debug class \ com \ getinstance \ util \ Debug: helloWorld () cannot be found; // after adding a slash, it will be searched from the root. // OutPut: hello from Debug?>
Use the use keyword
<?phpnamespace 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 foundutil\Debug::helloWorld();?>
You can call the class directly by using the following process:
<? Phpnamespace com \ getinstance \ util; class Debug {static function helloWorld () {print "hello from Debug \ n" ;}} namespace main; use com \ getinstance \ util \ Debug; // directly use the class Debug: helloWorld ();?>
\ Indicates global
Global. php
<? Php // no namespaceclass Lister {public static function helloWorld () {print "hello from global \ n" ;}}?> <? Phpnamespace com \ getinstance \ util; require_once 'Global. php'; class Lister {public static function helloWorld () {print "hello from ". _ NAMESPACE __. "\ n"; // _ NAMESPACE _ current namespace} Lister: helloWorld (); // access local \ Lister: helloWorld (); // access global?>
Output:
Hello from com \ getinstance \ util
Hello from global
Namespace plus {}
<?phpnamespace 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
<? Phpnamespace {// global space 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
<? Phpclass ShopProduct {function _ construct () {print "ShopProduct constructor \ n" ;}}?> <? Phpfunction _ autoload ($ classname) {// automatically load class include_once ("$ classname. php ") ;}$ product = new ShopProduct (The Darkening, 'Harry, Hunter, 12.99);?>
Output:
ShopProduct constructor
Further Optimization
In the business/ShopProduct. php folder
<? Phpclass business_ShopProduct {// The class name here must follow the rule function _ construct () {print "business_ShopProduct constructor \ n" ;}}?> <? Phpfunction _ autoload ($ classname) {$ path = str_replace ('_', DIRECTORY_SEPARATOR, $ classname); // intelligently processes require_once ("$ path. php ") ;}$ x = new ShopProduct (); $ y = new business_ShopProduct () ;?>
Output:
ShopProduct constructor
Business_ShopProduct constructor