php不经常用到的函数和类中的函数(一)

来源:互联网 发布:旅行哲学知乎 编辑:程序博客网 时间:2024/05/16 10:06

直接看例子:看不懂的要加油喽!

class people{
private $name;
private $age;

public function __construct($name,$age){
 $this->name = $name;
 $this->age = $age;
}

public function __toString(){
 return $this->name;
}

public function __set($key,$name){


}
//使用call魔术方法 当调用类中没有定义方法的时候 会直接调用 此魔术方法
public function __call($method, $arg){
  $method = "action".ucfirst($method);
  
  //function_exists(string $method) 检测方法是否存在
  //method_exists(object $obj,string $method) 检测类中的方法是否存在
  
  if(method_exists($this,$method)){//检测类中的方法是否存在
   call_user_func_array(array($this, $method), array("three", "four"));
  }
}

public function actionRuntest(){
$args = func_get_args();//获取传入的参数
$num = func_num_args(); //传进参数的个数
echo $num;
print_r($args);
}

}


$p1 = new people("chao",30); 
$p1->runtest("liuchao","xiaoming");


类中并没有声明runtest方法 但是通过别的方式就可以调用,并且不会报错哦!哈哈! 快点体验一下吧!



0 0
原创粉丝点击