Php Magic Method __set __get

Source: Internet
Author: User

__set calling the _set method when setting an inaccessible property

__get Call the _get method when getting an inaccessible property

<?PHPclassstu{Private $a; Private $b= 0;  public $c;  public $d= 0; //the private here can be replaced with protected public    Private function__get ($name) {        return123; }         //The private can also be replaced with protected public    Private function__set ($name,$value) {        Echo"this is set function"; }} $s=NewStu ();Echo"<pre>";Var_dump($s->a);//output:123Var_dump($s->b);//output:123Var_dump($s->c);//Output:nullVar_dump($s->d);//output:0Var_dump($s->e);//output:123 $s->a = 3;//Output:this is set functionEcho"<br>";$s->c = 3;//No outputEcho"<br>";$s->f = 3;//Output:this is set function

Look at the results

Php Magic Method __set __get

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.