2020年5月15日
<?phpclass A{const NAME = '黑子';public function getNAME(){return '类的内部:' . self::NAME;}}//在类外部获取属性//类名::常量名//::静态访问符var_dump(A::NAME);//在类内部获取属性//self和$this一样的意思,都是表达当前类;self表示静态 $this表示动态//self可以去获取变量和常量;$this只能去获取变量//self::常量名$obj = new A ();var_dump($obj->getNAME());function _autoload($classNAME){var_dump($classNAME);$rootPath = './org/';$fullFileNAME = $clasNAME . 'class.php';$fullPath =$rootPath . $fullFileNAME;if(file_exists($fullPath)){include_once $fullPath;}}$obj = new A();var_dump($obj);spl_autoload_register(function($className){$rootPath='./org/';$fullFileName=$NAME . '.class.php';$fullPath=$rootPath.$fullFileName;if(file_exists($fullPath)){include_once $fullPath;}});function putong($NAME){$rootPath='./org1/';$fullFileName=$NAME . '.class.php';$fullPath = $rootPath.$fullFileName;if(file_exists($fullPath)){if(file_exists($$fullPath)){include_once $fullPath;}}}spl_autoload_register('sdsdss');new A();new B();<?phpclass A{const NAME = '黑子';public function getNAME(){return '类的内部:' . self::NAME;}}//在类外部获取属性//类名::常量名//::静态访问符var_dump(A::NAME);//在类内部获取属性//self和$this一样的意思,都是表达当前类;self表示静态 $this表示动态//self可以去获取变量和常量;$this只能去获取变量//self::常量名$obj = new A ();var_dump($obj->getNAME());function _autoload($classNAME){var_dump($classNAME);$rootPath = './org/';$fullFileNAME = $clasNAME . 'class.php';$fullPath =$rootPath . $fullFileNAME;if(file_exists($fullPath)){include_once $fullPath;}}$obj = new A();var_dump($obj);spl_autoload_register(function($className){$rootPath='./org/';$fullFileName=$NAME . '.class.php';$fullPath=$rootPath.$fullFileName;if(file_exists($fullPath)){include_once $fullPath;}});function putong($NAME){$rootPath='./org1/';$fullFileName=$NAME . '.class.php';$fullPath = $rootPath.$fullFileName;if(file_exists($fullPath)){if(file_exists($$fullPath)){include_once $fullPath;}}}spl_autoload_register('sdsdss');new A();new B();<?phpclass A{const NAME = '黑子';public function getNAME(){return '类的内部:' . self::NAME;}}//在类外部获取属性//类名::常量名//::静态访问符var_dump(A::NAME);//在类内部获取属性//self和$this一样的意思,都是表达当前类;self表示静态 $this表示动态//self可以去获取变量和常量;$this只能去获取变量//self::常量名$obj = new A ();var_dump($obj->getNAME());function _autoload($classNAME){var_dump($classNAME);$rootPath = './org/';$fullFileNAME = $clasNAME . 'class.php';$fullPath =$rootPath . $fullFileNAME;if(file_exists($fullPath)){include_once $fullPath;}}$obj = new A();var_dump($obj);spl_autoload_register(function($className){$rootPath='./org/';$fullFileName=$NAME . '.class.php';$fullPath=$rootPath.$fullFileName;if(file_exists($fullPath)){include_once $fullPath;}});function putong($NAME){$rootPath='./org1/';$fullFileName=$NAME . '.class.php';$fullPath = $rootPath.$fullFileName;if(file_exists($fullPath)){if(file_exists($$fullPath)){include_once $fullPath;}}}spl_autoload_register('sdsdss');new A();new B();
类常量:
类常量使用const来定义
获取常量使用 类名 或 self
const 名字 = 值
__autoload(); 尝试加载未定义的类;只能定义一次
sql 是通过注册给定的函数 ;可以定义多条
类的自动加载
spl_autoload_register()
spl_autoload_register()实现的功能与_autoload 一致的
spl_autoload_register()这个的功能比_autoload 多一些;
多出的功能:
通过sql函数有多个自动加载类的函数(加载多个文件夹类)
通过列表的形式保存多个加载类的函数
spl_autoload_register
注册给定的函数作为 __autoload的实现
将函数注册到sql_autoload函数队列中,如果该队列中的函数未激活,则激活它
如果需要多条autoload函数,sql_autoload_register()满足了此类需求
按定义时的额顺序逐个执行
类的自动加载
为什么需要这个功能:
类=功能;由于有多个功能,如果写在一个文件中就不能方便的去写项目
mysql =>file
upload => file
page => file
自动加载规则:
前提:当new一个不存在(未定义)当前文件的类时,系统会将类名传递给类自动加载函数;
1.所有的类必须放在一个文件夹内
2.文件名字必须和类名一致(区分大小写)
3.文件的后缀必须为 .class.php栗子:类名.class.php
_autoload 自动加载函数
名字是固定,只有写一个自定义函数名字为_autoload 那他就是自动加载函数
spl_autoload_register 将一个函数注册成为自动加载类函数
