php learns the common functions of an array of notes
- 2020-05-07 19:26:30
- OfStack
<?php
/*
* Encapsulation: object oriented 3 Of the characteristics of the large 1
*
* 1. That's the member of the object ( attribute , methods ) Combined into 1 Two separate identical units, and hide as much internal detail as possible
* Access modifier public protected private
* private : private. Members that are decorated with this keyword can only be accessed within the object ( It is only with $this access )
*
* Properties can be encapsulated :
* As long as 1 If you need to use more than one method, declare the variable as a member property and use it directly in all the methods in the object
*
* A member property that corresponds to a global variable in this object
*
* Member properties are all used in methods, and changes in the value of a member property actually change the execution behavior of the method, that is, the function of the object
*
* If the value of the member property is abnormal, the function leaf of the method execution is abnormal
*
* Action: there is no need to change or read its value outside the object
* 1. encapsulation
* To provide 1 Public methods ( The method can be controlled by assigning and valuing the properties of object members )
*
*
* Methods can also be encapsulated
*
* Function:
* 1. use private Modifies it so that it can only be used internally
*
* 2.1 Have a class 100 Three methods, encapsulated 95 a ( For the other 5 Service methods ), only 5 Three methods can be used
*
* 100 Member attributes, all let value, can not change the value ; Or you can just change the value and not get the value // In this case, the following method is more convenient
* Magic methods related to encapsulation:
*
* __set();// It's a direct set [ private ] Method that is automatically called when a member property is valued
* __get();// Direct access [ private ] Method that is automatically called when a member property is valued
* __isset();// Direct use isset() This method is called automatically to see if private properties exist in the object
* __unset();// Direct use unset() The method that is automatically called when a private property in an object is deleted
*
*
*
*
*
*
*
*/
class Person{
//x Encapsulate member properties without changing them outside the object
private $name;
private $age;
private $sex;
private __unset($proName){
unset($this->$proName);
}
// This method is called automatically when you directly check to see if private properties exist in the object
//__isset($proName) The use of ,$proName Property name
private function __isset($proName){
return isset($this->$proName);//isset() Whether the return exists or not
}
function __construct($name,$age,$sex){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
// This method is called automatically when a private member property is obtained
private function __get($proName)
{
// Controls the values obtained
if($proName=="age"){
if($this-age>40)
return $this->age-10;
}
return $this->$proName;
}
// This method is called automatically when a private member property is set
private function __set($proName,$proValue){
//$proName Represents the member property name, $proValue Represents the value of a member property
// Control setting range
if($proName=="age"){
if($proValue > 100 || $proValue<0)
return;
}
$this->$proName=$proValue;
}
// Provides a public method to set the value of a member property
function setAge($age){
// Control the age range and increase safety
if($age > 100 || $age < 0 )
return;
$this->age=$age;
}
// Provides a public method to get the value of a member property
function getAge(){
// Control the range of acquisition ages
if($this->age < 30)
return $this->age;
else if($this->age<40)
return $this->age-5;
else if($this->age<50)
return $this->age;
else
return $this->age-15;
Provides public methods to }
function say(){
echo " My name: {$this->name} , my age: {$this->age} , my last name is: {$this->sex}<br>";
// Access encapsulated run() methods
$this-run();
}
private function run(){
echo '111111111111<br>'
}
function eat(){
}
// destructor
function __destruct(){
}
}
$p1=new Person("zhangsan",25," male ");
$p2=new Person;
$p3=new Person;
//$p1->age=-50;// Because the age in the external random access, so the member attribute drug encapsulation, there is security.
$p1->setAge(30);// Method to set the value of the member property
$p1->getAge();// Method to get the value of the member attribute
// By adding the home magic method __set($proName,$proValue) __get($proName) , you can call the member property directly
$p1->say();// You can call
$p1->run();// Private methods cannot be called directly
// delete $p1 The inside of the name
unset($p1->name);
// judge name If there is a
if(isset($p1->name)){
echo " There are <br>";
}else{
echo " There is no member <br>";
}
?>
Author: code name aurora
Reference: http: / / zizhuyuan. cnblogs. com