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