16.__toString() method
We have mentioned before that the method of declaring a method name starting with "-" in the class (provided by PHP to us) is automatically executed under different circumstances at a certain time
When calling the execution method, the "__toString()" method is also automatically called. It is automatically called when
directly outputs the object reference. Earlier we said that the object reference is a pointer, for example: "$p=new
Person()", $p is a reference. We cannot use echo to directly output $p. This will output an error like "Catchable fatal
error: Object of class Person could not be converted to string". If you define
the "__toString()" method in the class, no error will be generated when directly outputting the object reference. Instead, the
"__toString()" method will be automatically called and "__toString() will be output. )" method, so the "__toString()" method must
have a return value (return statement).
Code snippet
<?php // Declare a simple class class TestClass{ public $foo; public function __construct($foo) { $this->foo = $foo; } //定义一个__toString方法,返加一个成员属性$foo public function __toString() { return $this->foo; } } $class = new TestClass('Hello'); //直接输出对象 echo $class; ?>
The output of the above example: Hello
17. Clone object
Sometimes we need to use two or more identical objects in a project. If you use The "new"
keyword re-creates the object and then assigns the same attributes. This is more cumbersome and error-prone, so it is very necessary to
completely clone an identical object based on an object. , and after cloning, the two objects
will not interfere with each other.
In PHP5 we use the "clone" keyword to clone objects;
Code snippet
<? class Person{ //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值 function __construct($name="", $sex="", $age=""){ $this->name=$name; $this->sex=$sex; $this->age=$age; } //这个人可以说话的方法, 说出自己的属性 function say() { echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."<br>"; } } $p1=new Person("张三", "男", 20); //使用“clone”克隆新对象p2,和p1对象具有相同的属性和方法。 $p2=clone $p1; $p2->say(); ?>
PHP5 defines a special method name "__clone()" method, which is automatically used when the object is cloned The calling method,
Using the "__clone()" method will create an object with the same properties and methods as the original object. If you want to change the contents of the original object after cloning, you need to re-create it in __clone(). Write the original properties and methods. The "__clone()" method can have no parameters.
It automatically contains two pointers, $this and $that, $this points to the copy, and $that points to the original;
Code snippet
class Person{ //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值 function __construct($name="", $sex="", $age=""){ $this->name=$name; $this->sex=$sex; $this->age=$age; } //这个人可以说话的方法, 说出自己的属性 function say() { echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."<br>"; } //对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原本 的属性和方法 function __clone(){ //$this指的复本p2, 而$that是指向原本p1,这样就在本方法里,改变了复本的属性。 $this->name="我是假的$that->name"; $this->age=30; } } $p1=new Person("张三", "男", 20); $p2=clone $p1; $p1->say(); $p2->say(); ?>
Execution result
My name is: Zhang San Gender: Male My age is: 20
My name is: I am the fake Zhang San Gender: Male My age is: 30
18.__call Handling calling errors
In program development, if the method called does not exist when using an object to call an internal method of the object, then the program will
An error occurs, and then the program exits and cannot continue execution. So when the program calls a method that does not exist inside the object
, we are prompted that the method called and the parameters used do not exist, but the program can continue to execute. At this time we have to
use the method that does not exist in the call The method "__call()" is automatically called when the method is used.
Code snippet
<?php //这是一个测试的类,里面没有属性和方法 class Test{ } //产生一个Test类的对象 $test=new Test(); //调用对象里不存在的方法 $test->demo("one", "two", "three"); //程序不会执行到这里 echo "this is a test<br>"; ?>
Fatal error: Call to undefined method Test::demo()
Below we add "__call ()" method. This method has 2 parameters. The first parameter is to call a non-existent method.
During the process, when the __call() method is automatically called, the method name of the non-existent method is passed to the first parameter. parameter, the second parameter
is to pass in the multiple parameters of this method in the form of an array.
Code snippet
<?php //这是一个测试的类,里面没有属性和方法 class Test{ //调用不存的方法时自动调用的方法,第一个参数为方法名,第二个参数是数组参数 function __call($function_name, $args){ print "你所调用的函数:$function_name(参数:"; print_r($args); print ")不存在!<br>\n"; } } //产生一个Test类的对象 $test=new Test(); //调用对象里不存在的方法 $test->demo("one", "two", "three"); //程序不会退出可以执行到这里 echo "this is a test<br>"; ?>
Execution result
The function you called: demo(Parameter: Array ([0] => one [1] => ; two [2] => three ) ) does not exist!
This is a test.