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

Source: Internet
Author: User

Public represents the global, the internal external subclasses of the class can be accessed;

Copy Code code as follows:

<?php

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

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

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


$P =new Test ();
Echo ' <br/><br/> ';
$P->age=100;
$P->name= "Rainy";
$P->sex= "female";
$P->func ();
?>
Public

Private means proprietary, only within this class can be used;
Copy Code code as follows:

<?php

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

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

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

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


$P =new Test ();
Echo ' <br/><br/> ';
$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 () ""
?>
Private

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

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

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

 __isset () ; is direct isset automatically calls this method

 __unset () when a private property in an object is saved (), and is automatically invoked when a private property in an object is deleted directly unset

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.