Final / __autoload / Namespace

Final:终态的,最后的

Final修饰的类不能被继承

Final修饰的函数方法不能被重写

 

__autoload:

尝试加载未定义的类

该函数将在7.2.0中被设置为过期,并在以后的更高版本中删除

现在已经建议使用新版本的函数 spl_autoload_register

testClass.php的代码

$className 需要被加载的类名,字符串格式

Function __autoload($className){

Include_once($className. “.class.php”);

}

$teacher =new Teacher(“老师”);

Echo $teacher->getName();

以下是Teacher.class.php的代码

Class Teacher{

Private $name;

Function __construct($name){

$this->name =$name;

}

Public function getName(){

Return $this->name;

}

}

标准形式,定义一个函数,然后使用sql_autoload_register来注册到我们的自动加载中并激活

5.3.0开始,自动加载注册可以使用匿名函数来实现

testClass.php的代码

spl_autoload_register(
    function ($className){
        Include_once($className. '.class.php');
    }
);
$teacher =new Teacher('老师');
Echo $teacher->getName();

 

 

命名空间Namespace

Namespace必须声明在所有代码的最前面

使用use可以指定加载的命名空间及其对应的类,同时在类名后面可以使用as关键字指定某个类的别名

一旦起了别名之后,在当前文件中就可以使用这个别名来创建对应类的对象

例如:use lovoTestNamespace as MyTest;

$test= new MyTest();

$test->test();

原文地址:https://www.cnblogs.com/zhubaixue/p/7217508.html