Php magic method function and usage instance analysis, magic instance analysis

Source: Internet
Author: User

Php magic method function and usage instance analysis, magic instance analysis

This article describes the functions and usage of php magic methods. We will share this with you for your reference. The details are as follows:

<? Php // php magic method header ('content-type: text/html; charset = UTF-8 '); class Person {public $ name; protected $ sex; private $ salary; // constructor. The instantiated object is the automatically triggered public function _ construct ($ name, $ sex, $ salary) {$ this-> name = $ name; $ this-> sex = $ sex; $ this-> salary = $ salary;} // magic constant _ CLASS _ public function getClassName () {echo _ CLASS __;}// _ FUNCTION _ public function getMethod () {echo _ FUNCTION __;}// _ tostring () public function _ tostring () {return 'the object must use var_dump ()'. 'name is '. $ this-> name;} // _ clone () the public function _ clone () is automatically called when the clone method is used () {echo 'the object is cloned '; $ this-> name = 'lily';} // _ get () public function _ get ($ a) {echo $. 'Property does not exist or permission is insufficient ';} // _ set () the public function _ set ($ name, $ value) {echo $ name. 'Is '. $ value;} // _ isset () the public function _ isset ($ name) method is automatically triggered when an attribute does not exist or has insufficient permissions is determined outside the class) {echo $ name. 'cannot be blank';} // _ call () when a method with insufficient or nonexistent permissions is accessed, the magic method public function _ call ($ method, $ a) {echo 'method you requested '. $ method. 'nonexistent '; $ this-> getname ();} public function getname () {echo $ this-> name ;}} // instantiate the object $ person = new Person ('zhang san', 'mal', 12223); $ person1 = new Person ('zhang san', 'mal', 12223 ); // $ person2 = new Person ('lily', 'female', 11111); var_dump ($ person); var_dump ($ person1); exit; $ person-> getClassName (); $ person-> getMethod (); // echo $ person-> name; // echo $ person; $ person1 = $ person; // The same object // var_dump ($ person); // var_dump ($ person1); $ person2 = clone $ person; // The clone generates different objects // var_dump ($ person); // var_dump ($ person2); // echo $ person2-> name; // $ person-> salary = 10000; // var_dump (isset ($ person-> salary); // $ person-> getName ();

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.