Home > Backend Development > PHP Tutorial > PHP object-oriented guide (11) __toString() usage, cloning objects, __call handling calling errors_PHP tutorial

PHP object-oriented guide (11) __toString() usage, cloning objects, __call handling calling errors_PHP tutorial

WBOY
Release: 2016-07-21 15:44:04
Original
1006 people have browsed it

16.__toString() method
We mentioned earlier that the method of declaring a method name starting with "-" in the class (provided by PHP to us) is different at a certain time
In this case, the execution method is automatically called. 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 "Catchable fatal
error: Object of class Person could not be converted to string" like this Error, if you define the
"__toString()" method in the class, when directly outputting the object reference, no error will be generated, but the
"__toString()" method will be automatically called, and the output The characters returned in the "__toString()" method, so the "__toString()" method must
have a return value (return statement).
Code snippet

Copy code The code is as follows:

// Declare a simple class
class TestClass{
public $foo;
public function __construct($foo) {
$this->foo = $foo;
}
//Define a __toString Method, return a member attribute $foo
public function __toString() {
return $this->foo;
}
}
$class = new TestClass('Hello') ;
//Directly output the object
echo $class;
?>

The above example output: Hello
17. Clone object
Sometimes we You need to use two or more identical objects in a project. If you use the "new"
keyword to recreate the object, then assign the same attributes. This is more cumbersome and error-prone, so you need to
It is very necessary to completely clone an identical object from 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
Copy code The code is as follows:

class Person{
//The following are the member attributes of the person
var $name; //The name of the person
var $sex; //The gender of the person
var $age; //Age of a person
//Define a constructor parameter to assign values ​​to the attributes name $name, gender $sex and age $age
function __construct($name="", $sex ="", $age=""){
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
//The way this person can speak is to tell his attributes
function say() {
echo "My name is: ".$this->name." Gender: ".$this->sex." My age is: ".$this->age."
";
}
}
$p1=new Person( "Zhang San", "Male", 20);
//Use "clone" to clone the new object p2, which has the same properties and methods as the p1 object.
$p2=clone $p1;
$p2->say();
?>

PHP5 defines a special method name "__clone()" Method is a method that is automatically called when an object is cloned.
Using the "__clone()" method will create an object with the same properties and methods as the original object. If you want to change the content of the
original object after cloning, The original attributes and methods need to be rewritten in __clone(). 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 copy. Original;
Code snippet
Copy code The code is as follows:

class Person{
//The following is the person Member attributes of
var $name; //The person’s name
var $sex; //The person’s gender
var $age; //The person’s age
//Define a constructor parameter Assign values ​​to the attributes name $name, gender $sex and age $age
function __construct($name="", $sex="", $age=""){
$this->name= $name;
$this->sex=$sex;
$this->age=$age;
}
//The way this person can speak, tell his own attributes
function say() {
echo "My name is: ".$this->name." Gender: ".$this->sex." My age is: ".$this ->age."
";
}
//Method automatically called when an object is cloned. If you want to change the content of the original object after cloning, you need to rewrite the original object in __clone() Attributes and methods of

function __clone(){
//$this refers to the copy p2, and $that points to the original p1, so in this method, the attributes of the copy are changed.
$this->name="I am fake $that->name";
$this->age=30;
}
}
$p1=new Person("张三", "男", 20);
$p2=clone $p1;
$p1->say();
$p2->say();
?>

Output of the above example:
Execution result
My name is: Zhang San Gender: Male My age is: 20
My name is: I am fake Zhang San Gender: Male My age is: 30
18.__call Handling call 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 make an error, and then The program exits and cannot continue execution. So, when the program calls a method
that does not exist inside the object, it prompts us 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
Copy code The code is as follows:

//This is a test Class, there are no attributes and methods in it
class Test{
}
//Generate an object of Test class
$test=new Test();
//Does not exist in the calling object Method
$test->demo("one", "two", "three");
//The program will not execute here
echo "this is a test
";
?>

The following error occurs in the above example, and the program cannot continue to execute;
Fatal error: Call to undefined method Test::demo()
Let’s add below Go to the "__call()" method. This method has 2 parameters. The first parameter is to call the non-existent method
. During the process of automatically calling the __call() method, pass the method name of the non-existent method. For the first parameter, the second parameter
is to pass in the multiple parameters of this method in the form of an array.
Code snippet
Copy code The code is as follows:

//This is a test Class, there are no attributes and methods in it
class Test{
//Method that is automatically called when calling a non-existing method. The first parameter is the method name, and the second parameter is the array parameter
function __call ($function_name, $args){
print "The function you called: $function_name(parameters: ";
print_r($args);
print ") does not exist!
n" ;
}
}
//Generate an object of Test class
$test=new Test();
//Call methods that do not exist in the object
$test-> ;demo("one", "two", "three");
//The program will not exit and can be executed here
echo "this is a test
";
?>

The output result of the above example is:
Execution result
The function you called: demo(Parameter: Array ( [0] => one [1] => two [2 ] => three ) ) does not exist!
This is a test.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320630.htmlTechArticle16.__toString() method We have mentioned before that the method name starting with "-" is declared in the class ( PHP provides us), which are automatically called and executed under different circumstances at a certain time...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template