PHP notes: A detailed explanation of Object oriented Design
- 2020-06-03 05:56:43
- OfStack
public represents the global, which can be accessed by both internal and external subclasses.
<?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 is private and can only be used within this class;
<?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() from context ''
?>
Private
protected represents protected, accessible only in this class or subclass or superclass; Magic methods related to encapsulation:
___ () : Is the method that is automatically called when the private member property value is set directly
Succget () : Is the method that is automatically called when you get the value of the private member property directly
__isset (); This method is called automatically when isset checks to see if a private property in an object is saved
__unset (); Is called automatically when unset directly deletes a private property in an object