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 'my\Hello' not found
$hello = new Hello();
?>

<?php
// useful/Outputter.php
namespace useful; // 命名空间

class Outputter {
    // 
}

class Hello {
    
}
?>

如何调用命名空间中的类

<?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关键字

<?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();
?>

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

<?php

namespace com\getinstance\util;

class Debug {
    static function helloWorld() {
        print "hello from Debug\n";
    }
}

namespace main;
use com\getinstance\util\Debug; // 直接使用到类
Debug::helloWorld();
?>

\表示全局

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
?>

输出:
hello from com\getinstance\util
hello from global

命名空间加{}

<?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

全局命名空间

<?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 自动加载类

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

进一步优化处理

<?phpclass business_ShopProduct // 这里的类命名就要遵循规则了function __construct() print"business_ShopProduct constructor\n";?><?phpfunction __autoload( $classname ) $pathstr_replace('_',DIRECTORY_SEPARATOR,$classname);// 智能化处理require_once("$path.php");$xnew();$ynew();?>
相关文章
|
1月前
|
Java 程序员 PHP
PHP对象和类
PHP对象和类
21 0
|
6月前
|
SQL 安全 PHP
理解php对象注入
php对象注入是一个非常常见的漏洞,这个类型的漏洞虽然有些难以利用,但仍旧非常危险,为了理解这个漏洞,请读者具备基础的php知识。
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
php案例:自己写个数组转换成对象 对象转换成数组的的功能出来吧
|
3月前
|
JSON PHP 数据格式
|
9月前
|
存储 JSON PHP
在 PHP 中从 URL 获取 JSON 对象
在 PHP 中从 URL 获取 JSON 对象
|
9月前
|
JSON PHP 数据格式
php清洗数据实战案例(2):根据键值进行二维数据的对象数组的排序
php清洗数据实战案例(2):根据键值进行二维数据的对象数组的排序
55 0
|
9月前
|
定位技术 PHP
php基于百度地图封装的对象类实现计算地图上两点间的距离和地理编码
php基于百度地图封装的对象类实现计算地图上两点间的距离和地理编码
60 0
|
9月前
|
编解码 前端开发 JavaScript
layui框架实战案例(5):基于PHP后端的layUI上传视频到七牛云对象储存并自动转码
layui框架实战案例(5):基于PHP后端的layUI上传视频到七牛云对象储存并自动转码
197 0
|
9月前
|
PHP
PHP文件上传属性对象解读
PHP文件上传属性对象解读
42 0
|
10月前
|
存储 SQL 前端开发
PHP对象的引用及对象优化策略
当PHP5的出现面向对象思想,我们在构造PHP程序时候就有了对程序的新的总结,把对象特性封装到类,特别是当PHP框架应用到实际项目中,构造类的对象和调用类对象出现了很大的讲究。