PHP Notes: A detailed _php example based on object-oriented design

Source: Internet
Author: User
Public represents the global, and the inner and outer subclasses of the class can be accessed;
Copy CodeThe code is as follows:

Class test{
Public $name = ' janking ',
$sex = ' Male ',
$age = 23;

function __construct () {
echo $this->age. '
'. $this->name. '
'. $this->sex. '
';
}

function func () {
echo $this->age. '
'. $this->name. '
'. $this->sex. '
';
}
}


$P =new Test ();
Echo '

';
$P->age=100;
$P->name= "Rainy";
$P->sex= "female";
$P->func ();
?>
Public

Private means that only this class can be used internally;
Copy CodeThe code is as follows:

Class test{
Private $name = ' janking ',
$sex = ' Male ',
$age = 23;

function __construct () {
$this->funcone ();
}

function func () {
echo $this->age. '
'. $this->name. '
'. $this->sex. '
';
}

Private Function Funcone () {
echo $this->age. '
'. $this->name. '
'. $this->sex. '
';
}
}


$P =new Test ();
Echo '

';
$P->func ();
$P->age=100; Cannot access private property Test:: $age
$P->name= "Rainy"; Cannot access private property Test:: $name
$P->sex= "female"; Cannot access private property Test:: $female
$P->funcone (); Call to Private Method Test::funcone () from context '
?>
Private

Protected represents a protected, only accessible in this class or subclass or parent class, and encapsulation-related magic methods:

__set (): A method that is automatically called when a private member property value is set directly

__get (): method that is called automatically when a private member property value is obtained directly

__isset (); This method is called automatically when the private property in the object is isset to see if it is stored.

__unset (); Is the method that is called automatically when you delete a private property in an object directly unset

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