PHP object-oriented Strategy (11) __tostring () Usage Cloning object __call processing call error _php base

Source: Internet
Author: User
16.__tostring () method
We said earlier that the method of declaring "-" the name of the method in the class (provided by PHP) is in the
The "__tostring ()" method is also automatically invoked at a time when the method of executing is automatically invoked under different circumstances, and is
Called automatically when the object reference is directly output, we've talked before that an object reference is a pointer, for example, "$p =new
In person () ", $p is a reference, we cannot use echo to output $p directly, this will output" catchable fatal
Error:object of class person could the not is converted to string "Such an error if you define within the class
The "__tostring ()" method, when directly outputting an object reference, does not produce an error, but automatically invokes the
The "__tostring ()" method outputs the characters returned in the __tostring () method, so the "__tostring ()" Method must
You want to have a return value (returns statement).
Code fragment
Copy Code code as follows:

<?php
Declare a simple class
Class testclass{
Public $foo;
Public function __construct ($foo) {
$this->foo = $foo;
}
Defines a __tostring method that returns a member property $foo
Public Function __tostring () {
return $this->foo;
}
}
$class = new TestClass (' Hello ');
Direct Output Object
Echo $class;
?>

Previous example output: Hello
17. Cloning objects
Sometimes we need to use two or more of the same objects in a project, if you use "new"
Keyword to recreate the object, and then assign the same properties, this is cumbersome and error prone, so to
It is very necessary to completely clone an identical object from an object, and after cloning, two objects
Mutual interference.
In PHP5 we use the "clone" keyword to clone the object;
Code fragment
Copy Code code as follows:

?
Class person{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
Define a constructor parameter to assign the attribute name $name, gender $sex, and age $age
function __construct ($name = "", $sex = "", $age = "") {
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
This person can speak the way to speak his own attributes
function say () {
echo "My name is called:" $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
}
$p 1=new person ("John", "Male", 20);
Cloning new object P2 with clone has the same properties and methods as the P1 object.
$p 2=clone $p 1;
$p 2->say ();
?>

PHP5 defines a special method name "__clone ()" method, which is automatically invoked when an object is cloned.
Using the __clone () method creates an object with the same properties and methods as the original object, if you want to change after cloning
The contents of the original object, you need to rewrite the original properties and Methods in __clone (), the "__clone ()" method can have no parameters,
It automatically contains $this and $that two pointers, $this pointing to a replica, while the $that points to the original;
Code fragment
Copy Code code as follows:

Class person{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
Define a constructor parameter to assign the attribute name $name, gender $sex, and age $age
function __construct ($name = "", $sex = "", $age = "") {
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
This person can speak the way to speak his own attributes
function say () {
echo "My name is called:" $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
The method that is invoked automatically when the object is cloned, and if you want to change the contents of the original object after cloning, you need to rewrite it in __clone ()
The properties and methods
function __clone () {
The copy of the $this refers to the P2, and the $that is pointing to the original P1, so that in this method, the properties of the copy are changed.
$this->name= "I am a false $that->name";
$this->age=30;
}
}
$p 1=new person ("John", "Male", 20);
$p 2=clone $p 1;
$p 1->say ();
$p 2->say ();
?>

Previous example output:
Execution results
My name is called: John Sex: Male My age is: 20
My name is called: I am false john Sex: Male My age is: 30
18.__call Processing Call Error
In program development, the method invoked when using an object to invoke an object's internal method does not have a path
The sequence will go wrong, and then the program exits and cannot continue. So is it possible to call a method that does not exist inside the object
, we are prompted to invoke the method and the parameters used do not exist, but the program can continue to execute, this time we will
Use the method "__call ()" That is invoked automatically when a method that does not exist is invoked.
Code fragment
Copy Code code as follows:

<?php
This is a test class that has no attributes or methods in it.
Class test{
}
Object that produces a test class
$test =new test ();
Methods that do not exist in the calling object
$test->demo ("One", "two", "three");
The program will not execute here
echo "This is a test<br>";
?>

The following error occurred in the above example, the program can not continue to pass the execution;
Fatal error:call to undefined method Test::d Emo ()
Here we add the "__call ()" method, which has 2 parameters, and the first parameter is a method that does not exist.
Procedure, when the __call () method is invoked automatically, the method name of the nonexistent method is passed to the first argument, and the second parameter
The number is to pass multiple parameters of this method in the form of an array.
Code fragment
Copy Code code as follows:

<?php
This is a test class that has no attributes or methods in it.
Class test{
The method that is invoked automatically when the method is not saved, the first argument is the method name, and the second argument is the array parameter
function __call ($function _name, $args) {
Print "The function you called: $function _name (parameter:";
Print_r ($args);
Print ") does not exist! <br>\n ";
}
}
Object that produces a test class
$test =new test ();
Methods that do not exist in the calling object
$test->demo ("One", "two", "three");
Program does not exit can be executed here
echo "This is a test<br>";
?>

The above example outputs the following results:
Execution results
The function you called: Demo (parameter: Array ([0] => one [1] => two [2] => three)) does not exist!
This is a test.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.