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

  • 相关阅读:
    JS解析XML文件和XML字符串
    查询优化的方法
    Oracle 常用操作
    取得同一网段内的IP和MAC地址!
    域名知多少?
    Oracle 数据库链路 同义词
    提高查询速度的方法【百万级以上数据】
    ExtJs学习之路从Grid中得到数据
    一个左边停靠且可以展开和隐藏的菜单【Jquery插件】
    Go流程控制
  • 原文地址:https://www.cnblogs.com/zhubaixue/p/7217508.html
Copyright © 2020-2023  润新知