<?php
/*
* 手动加载
*/
include_once 'SELF.php';
$s=new Saler();
/*
* 加载类文件是比较消耗资源的方式,所以有时候不确定是否在内存中存在的话
* 先使用class_exists()函数来判断是否存在,存在是不用加载,不存在再加载
*/
if(!class_exists('Saler')){
include_once 'SELF.php';
}
$s=new Saler();
/*
* 自动加载:PHP提供了一种加载机制:实现定义
* 一个函数_autoload(),然后当系统需要使用类,而
* 内存中又不存在的时候,系统就会自动调用_autoload()函数来加载类文件
*/
// function __autoload($classname){
// //需要定义去哪里加载,怎么加载
// include_once $classname .'php';//定义当前目录下,类文件名为:类名.php
// }
// $fn=new Saler();
/*
* 一个系统里,可能类文件会放到不同的路径下,
* 因此一个完整的自动加载函数,
* 应该要进行文件判定以及加载功能
*/
function __autoload($classname){
//组织文件路径,
$c_file='../'.$classname.'php';
if (file_exists($c_file)){
include_once $c_file;
}else{
$b_file=$classname.'php';
if (file_exists($b_file)){
include_once $b_file;
}
}
}
/*
* php7之后不怎么建议去直接使用_autoload()函数,而是采用一种注册机制,
* 将用户自定义的函数放到系统内部,
* 使用spl_autoload_register(定义好的函数)
*
*/
//自己定义一个函数名称
function c_autoload($classname){
$c_file='../'.$classname.'php';
if (file_exists($c_file)){
include_once $c_file;
}
}
function m_mautoload($classname){
$m_file='../'.$classname.'php';
if (file_exists($m_file)){
include $m_file;
}
}
spl_autoload_register('c_autoload');
spl_autoload_register('m_mautoload');
?>
本博客所有文章除特别声明外,均采用
CC BY-NC-SA 4.0
许可协议。转载请注明来自喵喵知识园 !
评论
评论