PHP __construct (), __destory (), __get (), __set (), __call (), _php tutorial

Source: Internet
Author: User
(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 ...

  • 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.