批改状态:合格
老师批语:
重载方法必须声明为public
重载分为:属性重载和方法重载
属性重载:__get();__set():__isset();__unset();
方法重载:__call();静态方法__callstatic();
使用方法__call($method.$arguments);//方法名,数组
使用方法__callstatic($method.$arguments)
<?php
//重载方法必须声明为public
//重载分为:属性重载和方法重载
//属性重载:__get();__set():__isset();__unset();
//方法重载:__call();静态方法__callstatic();
//使用方法__call($method.$arguments);
//使用方法__callstatic($method.$arguments)
echo '__call调用不可访问或不存在的方法时被调用<br>';
echo '__callStatic调用不可访问或不存在的静态方法时被调用<hr>';
class ClassTest
{
public function __call($method,$arguments) //创建一个不存在的方法
{
/*return $method; //方法名称
return $arguments;//数组值*/
$args =implode(',', $arguments); //implode将数组中元素切割为字符串
return '方法名:'.$method.'<br>参数:'.$args;
}
private function select($a) //创建一个私有方法
{
return __METHOD__;
return $this->$a;
}
public static function __callStatic($a,$b) //创建一个静态的不存在的方法
{
$args =implode(',', $b); //implode将数组中元素切割为字符串
return '方法名:'.$a.'<br>参数:'.$args;
}
}
//实例化对象
$ClassTest = new ClassTest();
//输出一个不存在的方法
echo $ClassTest->show('南京','上海','北京').'<hr>';
//输出一个私有方法
echo $ClassTest->select('a','1','3').'<hr>';
//从外部访问一个不存在的静态方法
echo ClassTest::abc('1','2','3');
?>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号