PHP Magic Method __get __set (ii)

Source: Internet
Author: User

Slowly long search night, the moon hanging

__get ()-__get () is called when the value of the Inaccessible property is read

__set ()-__set () is called when a value is assigned to an unreachable property

<?php/** * Clear understanding of __get () __set () */class Example {        //public properties    $public = ' pub ';    Protected-the attribute in the subclass can be    protected $protected = ' Pro ';    Private-only This class uses this property    private $private = ' pri ';        The __get () method is loaded automatically when a property in the Access object does not exist or is not a public attribute    __get ($name) {        return ' calls the __get () method: '. $name;    }        When assigning a value to an object's property, the __set () method is loaded automatically if the property does not exist or is not a public property ()    __set ($name, $value) {        echo "\nname:". $ Name. ', Value: '. $value. ' \ n ";    }} $example = new Example;echo ' <pre> '; Echo $example->public. " \ n "; Echo $example->protected." \ n "; Echo $example->private." \ n "; Echo $example->other." \ n "; Echo ' 

The results are as follows:

Pub calls the __get () method: protected Call the __get () method: Private Call the __get () method: Othername: protected, Value:tecname:private, value:vatname:other, value:   Public Property: Lic

PHP Magic Method __get __set (ii)

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.