php autoload用法,php __autoload()的用法

这篇博客介绍了PHP中的魔术方法`__call()`和`__toString()`的应用。`__call()`在调用不存在的方法时触发,`__toString()`用于对象转化为字符串时返回私有变量的值。同时,文章还讲解了`__autoload()`函数在类自动加载中的作用,确保类文件在需要时被正确引入。
摘要由CSDN通过智能技术生成

class SportObject9{

public function myDream(){

echo '调用的方法存在,直接执行此方法。
';

}

public function __call($name, $arguments)

{

// TODO: Implement __call() method.

echo '方法不存在,则执行__call()方法。
';

echo '方法名为:'.$name.'
';

echo '参数有:';

echo '

';

print_r($arguments);

}

}

$exam=new SportObject9();

$exam->myDream();

$exam->mDream('how','what','why');

/**

* 调用的方法存在,直接执行此方法。

方法不存在,则执行__call()方法。

方法名为:mDream

参数有:

Array

(

[0] => how

[1] => what

[2] => why

)

*/

class SportObject10{

private $type='DIY';

public function __toString()

{

return $this->type;//方法返回私有变量$tpye的值

// TODO: Implement __toString() method.

}

}

$mycomputer=new SportObject10();

echo '对象$mycomputer的值为:'.$mycomputer.'
';

/**

* 对象$mycomputer的值为:DIY

*/

/**

* 通常用include()或require()函数在一个php文件中引入类文件。‘

* require('a.php');

* $a=new a();

*/

function __autoload($classname){

$class_path=$classname.'php文件路径';

if (exists($class_path)){

include_once ($class_path);

}else{

echo '类路径错误';

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值