zoukankan      html  css  js  c++  java
  • PHP面向对象深入研究之【命名空间】与【自动加载类】

    命名空间

    避免类名重复,而产生错误。

    <?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 'myHello' not found
    $hello = new Hello();
    ?>
    
    <?php
    // useful/Outputter.php
    namespace useful; // 命名空间
    
    class Outputter {
        // 
    }
    
    class Hello {
    	
    }
    ?>
    
    

    如何调用命名空间中的类

    <?php
    
    namespace comgetinstanceutil;
    
    class Debug {
        static function helloWorld() {
            print "hello from Debug
    ";
        }
    }
    
    namespace main;
    // comgetinstanceutilDebug::helloWorld(); // 找不到Debug类
    comgetinstanceutilDebug::helloWorld(); // 加斜杠之后,就从根部去寻找了。
    
    // outPut:hello from Debug
    ?>
    
    

    使用use关键字

    <?php
    
    namespace comgetinstanceutil;
    
    class Debug {
        static function helloWorld() {
            print "hello from Debug
    ";
        }
    }
    
    namespace main;
    use comgetinstanceutil;
    //Debug::helloWorld(); //Fatal error: Class 'mainDebug' not found 
    utilDebug::helloWorld();
    ?>
    
    

    使用下面的处理,直接可以调用类

    <?php
    
    namespace comgetinstanceutil;
    
    class Debug {
        static function helloWorld() {
            print "hello from Debug
    ";
        }
    }
    
    namespace main;
    use comgetinstanceutilDebug; // 直接使用到类
    Debug::helloWorld();
    ?>
    
    

    表示全局

    global.php
    <?php
    // no namespace
    
    class Lister {
        public static function helloWorld() {
            print "hello from global
    ";
        }
    }
    ?>
    
    <?php
    namespace comgetinstanceutil;
    require_once 'global.php';
    class Lister {
        public static function helloWorld() {
            print "hello from ".__NAMESPACE__."
    ";  // __NAMESPACE__当前namespace
        }
    }
    
    Lister::helloWorld();  // access local
    Lister::helloWorld(); // access global
    ?>
    
    输出:
    hello from comgetinstanceutil
    hello from global
    
    

    命名空间加{}

    <?php
    namespace comgetinstanceutil {
        class Debug {
            static function helloWorld() {
                print "hello from Debug
    ";
            }
        }
    }
    
    namespace main {
        comgetinstanceutilDebug::helloWorld();
    }
    ?>
    output:
    hello from Debug
    

    全局命名空间

    <?php
    namespace { // 全局空间
        class Lister {
            public static function helloWorld() {
                print "hello from global
    ";
            }
        }
    }
    namespace comgetinstanceutil {
        class Lister {
            public static function helloWorld() {
                print "hello from ".__NAMESPACE__."
    ";
            }
        }
    
        Lister::helloWorld();  // access local
        Lister::helloWorld(); // access global
    }
    ?>
    
    
    

    __autoload 自动加载类

    ShopProduct.php
    <?php
    
    class ShopProduct {
        function __construct() {
            print "ShopProduct constructor
    ";
        }
    }
    ?>
    
    <?php
    function __autoload( $classname ) { // 自动加载,根据类名加载类
        include_once( "$classname.php" );
    }
    $product = new ShopProduct( 'The Darkening', 'Harry', 'Hunter', 12.99 );
    ?>
    
    output:
    ShopProduct constructor
    
    

    进一步优化处理

    位于文件夹business/ShopProduct.php
    <?php
    class business_ShopProduct { // 这里的类命名就要遵循规则了
        function __construct() {
            print "business_ShopProduct constructor
    ";
        }
    }
    ?>
    
    <?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
    
  • 相关阅读:
    原创【cocos2d-x】CCMenuItemToggle 在lua中的使用
    SQL Server之LEFT JOIN、RIGHT LOIN、INNER JOIN的区别
    VS的IISExpress配置通过IP访问程序
    SQLServer执行大脚本文件时,提示“无法执行脚本没有足够的内存继续执行程序 (mscorlib)”
    jqGrid中multiselect: true 操作checkbox
    display:table的几个用法(元素平分宽度,垂直居中)
    ASP.NET中 前后台方法的相互调用
    AspxGridView使用手记
    大量文本框非空判断,如何提高灵活性?
    Mysql安装、配置、优化
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/5171215.html
Copyright © 2011-2022 走看看