Packaging:
Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding.
The above operation is to rely on the following access control symbols to achieve, set their own permissions, so that some people can access, and some people can not access
Routines:
[PHP]View PlainCopy
- <span style="FONT-SIZE:18PX;" ><?php
- Class person{
- public $name;
- private $age;
- private $salary;
- function __construct ($name,$age,$salary) {
- $this->name=$name;
- $this->age=$age;
- $this->salary=$salary;
- }
- }
- $a 1=new person ("xiaoming", 32,5000);
- Correct display, no restrictions on global property access
- echo "<br>". $a 1->name. "<br>";
- Error, private property cannot be accessed outside the class
- echo "<br>" $a 1->age. " <br> ";
- Error, private property cannot be accessed outside the class
- echo "<br>" $a 1->salary. " <br> ";
- ?></span>
PHP provides three types of access control symbols to control access to methods and properties (variables).
1. Public indicates global, can be used in this class, outside of class, subclass
2. Protected is protected, can be used in this class, subclass, cannot use n outside the class
3. Private means proprietary, can only be used in this class, cannot be used outside the class, that is, new in other places, and cannot call the method
These three control modifier symbols can be decorated with attributes and methods
1. If a method does not have an access modifier, the default is public
2. The attribute must specify an access modifier, and if there is no access modifier, an error is given.
3. The private method is best preceded by an underscore, such as: Private Function _save () {}
Methods can be called to each other. But you need to use $this reference
function test11 ()
{
$this->test12 ();
}
protected function test12 ()
{
echo "test12 Hello";
}
To illustrate:
[PHP]View PlainCopy
- <?php
- Class person1{
- public $name;
- protected $age;
- private $salary;
- function __construct ($name,$age,$salary) {
- $this->name=$name;
- $this->age=$age;
- $this->salary=$salary;
- }
- //Member Method
- Public function Showinfo () {
- //Here is a description of the variables that can be modified in this class using public protected and private.
- echo $this->name."| |". $this->age. "||". $this->salary;
- }
- function Test () {
- //Call the Showinfo () method and use this reference
- $this->showinfo ();
- }
- }
- Create a person
- $p 1=New Person1 ("Monkey King", 30,1000);
- $p 1->test ();
- ?>
To access the protected variable or private variable outside of the class, it is common practice to create a public method in the class to access the variables. form is
Public Function setxxx ($val) {
}
Public Function getXxx () {
}
Public Function Showinfo () {
}
To illustrate:
[PHP]View PlainCopy
- <?php
- Class person2{
- public $name;
- protected $age;
- private $salary;
- function __construct ($name,$age,$salary) {
- $this->name=$name;
- $this->age=$age;
- $this->salary=$salary;
- }
- //Member Method
- Public function Showinfo () {
- //Here is a description of the variables that can be modified in this class using public protected and private.
- echo "<br>". $this->name. "||". $this->age. "||". $this->salary;
- }
- }
- Create a person
- $p 1=New Person2 ("Pig", 20,5000);
- Access to its details, as the attributes age and salary are protected and private variables, if accessed directly outside of the class
- Its properties are error-able, but a function that creates a public property in a class accesses these variables through a function.
- $p 1->showinfo ();
- ?>
PHP Object-oriented – encapsulation