php __autoload()的用法

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-1 12:25   837   0
class SportObject9{
        public function myDream(){
            echo '调用的方法存在,直接执行此方法。<br>';
        }
        public function __call($name, $arguments)
        {
            // TODO: Implement __call() method.
            echo '方法不存在,则执行__call()方法。<br>';
            echo '方法名为:'.$name.'<br>';
            echo '参数有:';
            echo '<pre>';
            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.'<br>';
/**
 * 对象$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 '类路径错误';
        }
    }

转载于:https://my.oschina.net/u/3647818/blog/3033317

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP