PHP object-oriented part 2 - encapsulation
Dec 05, 2016 pm 01:26 PM1.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.

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
