(1) __construct () is a PHP built-in constructor that is automatically invoked with the PHP parsing engine, which is invoked first when an object is instantiated.
Example: Class Test
{
function __construct ()
{
echo "This is __construct function!";
}
function Test ()
{
echo "This is test!";
}
}
$objTest = new Test; The result of the operation was "this is __construct function!"
(2) __destory () is a built-in destructor for PHP, which is called when an object or object operation is dropped, so operations such as freeing resources can be done.
Class Test
{
function __destory ()
{
echo "This is __destory function!";
}
}
$objTest = new Test; The result of the operation was "this is __destory function!"
(3) __get () is called when attempting to read a property that does not exist, similar to the various operations reflected in Java.
Class Test
{
function __get ($key)
{
Echo $key, "doesn ' t exist!";
}
}
$objTest = new Test;
$objTest->name; The run result is "Name does ' nt exist!"
(4) __set () is called when an attempt is made to write a value to a property that does not exist.
Class Test
{
function __set ($key, $val)
{
echo "Can ' t assign\" ". $val. "\" to ". $key;
}
}
$objTest = new Test;
$objTest->name = "Ljlwill"; The run result is "Can ' t Assign" Ljlwill "to Name"
(5) __call () Call this method when attempting to invoke a method that does not exist for an object.
Class Test
{
function __call ($key, $args)
{
echo "the function \" ". $key. " \ "doesn ' t exist. It's args are. Print_r ($args);
}
}
$objTest = new Test;
$objTest->getname ("2004", "Ljlwill");
The operation result is the function "getName" doesn ' t exist. It ' s args are:array (
[0] = 2004;
[1] = = Ljlwill;
)
(6) __tostring () is called when printing an object, similar to the ToString method of Java, when we directly print the object callback with this function.
Class Test
{
function __tostring ()
{
Return "This is test!";
}
}
$objTest = new Test;
Eho $objTest; The result of the operation was "this is test!"
(7) __clone () is called when the object is cloned.
Class Test
{
function __clone ()
{
echo "I am cloned!";
}
}
$objTest = new Test;
$objCloneTest = Clone $objTest; Operation result is "I am cloned!"
http://www.bkjia.com/PHPjc/478729.html www.bkjia.com true http://www.bkjia.com/PHPjc/478729.html techarticle (1) __construct () is a PHP built-in constructor that is automatically invoked with the PHP parsing engine, which is invoked first when an object is instantiated. Example: C ...