The difference between public, protected, and private
Public represents the global, and the inner and outer subclasses of the class can be accessed;
Protected is protected and is accessible only in this class or subclass or in the parent class;
Private means that only this class can be used internally;
Among them, public has global and inheritance; protected is inherited; private can only be used by this class, not global and inherited.
Usage of __set () and __get ()
If an external program wants to get a private property inside the class, such as a private $name, you can define a __get ($a) method in the class to manipulate the $name value and return it, and the external object can then get a processed private $name value through $test->name.
If an external program wants to operate on a private property inside a class, you can define a __set ($a, $b) method in the class to manipulate the private property, and the external object is then able to manipulate the $name property by $test->name= "value".
For example:
Class test{
Private $name = "";
function __construct () {
$this->name= "Alice";
$this->age=20;
}
}
function __set ($a, $b) {
$this $a = $b;
}
$test =new test ();
echo $test->name. " <br> ". $test->age." <br> ";
$test->name= "ABCD";
Echo $test->name;
?>
The output is:
Alice
20
Abcd
Differences between public, protected, and private, and the use of __set () and __get ()