Php Magic Method __get, __set use instance, __get__set_php tutorial

Source: Internet
Author: User

Php Magic Method __get, __set use instance, __get__set


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

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

Copy the Code code as follows:
<?php

/**
* Clear understanding of __get () __set ()
*/
Class Example {

Properties of the public
Public $public = ' pub ';
Protected-This attribute is available in subclasses
protected $protected = ' Pro ';
Private-this property can only be used by this class
Private $private = ' pri ';

The __get () method is loaded automatically when a property in the Access object does not exist or is not a public property
Public Function __get ($name) {
Return ' Call the __get () method: '. $name;
}

When assigning a value to an attribute of an object, the __set () method is loaded automatically if the property does not exist or is not a public property
Public Function __set ($name, $value) {
echo "\nname:". $name. ', Value: '. $value. " \ n ";
}
}

$example = new example;
Echo '

';
echo $example->public. " \ n ";
echo $example->protected. " \ n ";
echo $example->private. " \ n ";
echo $example->other. " \ n ";
Echo ';
$example->public = ' lic '; This assignment succeeds all not showing
$example->protected = ' tec ';
$example->private = ' VAT ';
$example->other = ' er ';
Echo '
';
Echo ' Print public properties: '. $example->public;

The results are as follows:

Copy the Code code as follows:
Pub
Call the __get () method: protected
Call the __get () method: Private
Call the __get () method: Other

Name:protected,value:tec

Name:private,value:vat

Name:other,value:er

Print Public Property: Lic

http://www.bkjia.com/PHPjc/917032.html www.bkjia.com true http://www.bkjia.com/PHPjc/917032.html techarticle php Magic Method __get, __set use instance, __get__set __get ()-When reading the value of an inaccessible property, __get () is called. __set ()-When assigning a value to an inaccessible property, __set () is ...

  • Related Article

    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.