Home  >  Article  >  php教程  >  PHP object-oriented part 2 - encapsulation

PHP object-oriented part 2 - encapsulation

WBOY
WBOYOriginal
2016-12-05 13:26:271216browse

1.Package:

When I talk about the specific concept, I feel like I don’t know what it is. In fact, it is

Add modifiers like public protected private in front of the member properties and member methods of the class

Purpose:Hide the internal details of the object as much as possible to achieve access control. [Not denying access]. (Form a barrier to the outside world and only retain limited external interfaces to communicate with the outside world.)

2. Principle of encapsulation:

The external part of the object cannot access the internal data (member attributes and member methods) of the object at will, thus effectively avoiding the "cross-infection" of external errors, enabling software errors to be localized, and greatly reducing the need for detection. Errors and difficulty of troubleshooting.

3. Modifier public protected private

public: public, default protected: protected private: private

4. Access scope


Whether the corresponding properties and methods can be accessed

public protected private
External to class            ×            ×
Internals of class

①Only public properties and methods can be accessed outside the class.

②Other properties and methods can be accessed indirectly by declaring public methods inside the class.

③You can access public, private, and protected properties and methods inside the class.

④Private and protected properties and methods cannot be accessed inside a class.

If the member method does not have any access control characters, it is public by default and can be accessed anywhere. (Public methods can be used as access interfaces outside the object to indirectly access the internal details of the object).

5. About several common magic methods in encapsulation __set(), __get(), __isset(), __unset()

Introducing these magic methods, first of all, the constructor method __construct() is not modified with keywords and is a public method (Do not set the constructor to private permissions)Users can use the constructor method in the instance After converting the object, assign initial values ​​to the private properties.

However, we have instantiated the object and want to assign values ​​to private properties while the program is running. There are two methods

(1). Set public methods inside the class to provide assignment and value interfaces

class Person{
    private $name="紫藤";
    
    public function __construct(){
        echo $this->name;
    }    
    
    //赋值方法
     public function setvalue($name){
           $this->name=$name;          
     }
      public function getvalue(){
           echo $this->name;       
     }
}

//实例化
$mod = new Person();

The result is: Wisteria

$mod->setvalue('宝强');
$mod->getvalue();

The result is: Baoqiang

(2)Magic methods__set(),__get(),__isset(),__unset()

If you have to set the public method every time like the above, it is very cumbersome. In versions after php5.1.0, __set() and __get() are predefined to complete all non-public attributes. Get and copy operations.

__set($propertyName,$propertyValue)

has two parameters, attribute name and attribute value . When assigning a value to a non-public attribute, this method will be automatically called ( means that __set() must be declared in the class) (Added later in the code )

__get($propertyName) will automatically call this method when getting the value of a non-public property.

Detect whether non-public attributes exist __isset() and delete the non-public attributes __unset() of an object. In order to prevent users from actively calling these two methods, use the private keyword to encapsulate them in the object.

__isset($propertyName) detects whether non-public attributes exist. (This is an auxiliary detection function and has no detection function). This method will only be automatically called when the isset() function is called externally.

__unset($propertyName) deletes the non-public properties of an object_. This method will only be automatically called when the unset() function is called externally.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn