php做自动注册脚本,PHP 中注册自定义autoload自动装载函数

使用php的spl函数spl_autoload_register(mixed)

//同很多注册函数一样,spl_autoload有多种注册方式

//php自带的自动装载函数

// function __autoload($class) {

//     include_once 'classes/' . $class . '.class.php';  //受用include_once,防止重复导入

// }

function my_autoloader($class) {

include_once 'classes/' . $class . '.class.php';

}

spl_autoload_register('my_autoloader');

//使用匿名函数进行注册

spl_autoload_register(function ($class) {

include_once 'classes/' . $class . '.class.php';

});

?>

也可以使用类方法

/wwwroot/webapp/classes/User.class.php

class User

{

const DISCRIPTION = 'User Class';

public static $classID = 1024;

private $name;

private $age;

private $gender;

public function __construct($name,$age,$gender)

{

$this->name = $name;

$this->gender = $gender;

$this->age = $age;

}

public function __set($property_name,$property_value)

{

if(property_exists(__CLASS__,$property_name))

{

$this->$property_name = $property_value;

}

else

{

$this->$property_name = $property_value;

}

}

public function __get($property_name)

{

if(property_exists(__CLASS__,$property_name))

{

return $this->$property_name;

}

else

{

return 'undefined';

}

}

public function __toString()

{

return self::DISCRIPTION;

}

public function __destruct()

{

//echo '
'.self::DISCRIPTION.'['.self::$classID.']'.'@destroy';

unset($this->name);

unset($this->age);

unset($this->gender);

}

}

/wwwroot/webapp/UserAction.php

class SPL_EXTEND

{

public static function autoload($class)

{

include_once 'classes/' . $class . '.class.php';

}

}

//这里直接使用类方法注册

spl_autoload_register(array('SPL_EXTEND','autoload'));

$u = new User('zhangsan',20,'男');

$u->name = 'lisi';

$u->undefined = 'undefined';

echo $u->test;

echo User::DISCRIPTION;

echo $u;

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值