//__tostring() 用于屏蔽错误信息,当需要输出错误信息时,用本函数内返回的字符串代替
//__invoke() 用于屏蔽错误信息,当把对象当作函数调用时,会执行本函数
<?php header("Content-type: text/html; charset=utf-8"); //设置中国时区 date_default_timezone_set('PRC'); // $str='mypic.jpg'; // echo strlen($str); // echo date('YmdHis').substr($str,stripos($str,'.'),strlen($str)-stripos($str,'.'))."<br>"; // echo md5('123'); class Student { protected $name="Karl"; private $age = 24; public $edu = "大专"; public function showInfo() { echo "<h2>遍历对象属性</h2>"; foreach($this as $key=>$value){ echo "\$this->{$key} = {$value}<br>"; } } public function __tostring() { return "Happy birthday!"; } public function __invoke() { return "I come from invoke!"; } } $obj1 = new Student(); // $obj2 = clone $obj1; // $obj3 = clone $obj1; // var_dump($obj1,$obj2,$obj3); // $obj1->showInfo(); var_dump($obj1); echo $obj1(); // echo 110+"abc";
标签:__,obj1,invoke,echo,tostring,public From: https://www.cnblogs.com/hnqyliu/p/16748486.html