[Mr. Mak] Common magic methods and keywords in PHP object-oriented

Source: Internet
Author: User
Tags constant definition

Mr. Mak introduction: In Learning PHP object-oriented programming ideas, we will encounter a lot of automatic call magic method, each magic method has its own specific triggering scenario, but its fundamental purpose is to write and actual use of the process to prevent the program error;

Common magic methods are:music=> Gusic (based on the initials Pinyin capitalized by Gusic Lenovo to music) as well as constructs, destructors, and special keywords Fsci-cats and AI

Magic Method Trigger Scenario

<!-- Gusic-------------------------------------------------------------------------------------------------------Start-------- ---->

__get the Magic method is automatically triggered when a non-public or nonexistent property is obtained outside the class

__unset the Magic method is automatically triggered when a non-public or nonexistent property is attempted to be destroyed outside of the class

__set ($name, $value) This method is automatically triggered when a value is re-assigned to an internal non-public or nonexistent property outside of the class

__isset ($name) automatically triggers this method when it detects a non-public or nonexistent property value

__call the Magic method is automatically triggered when a non-public or nonexistent member method is called

<!-- Gusic---------------------------------------------------------------------------------------------------------End-------- ----->

__construct () {} Automatically triggers the Magic method when instantiating a class with the New keyword (common)

__destruct destructor: 1, when the text is executed, 2, the variable is destroyed with unset, 3, and the member property is assigned a value;

About permission issues when member properties and methods are encapsulated:

The inner class of the outer subclass of the

Public may be able to

Private can not be able to

Protected No, you can't.

Some special keywords and magic methods

<!-- Fsci-cats------------------------------------------------------------------------------------------------------------Star T------------->

Introduction to Name

F = Final version terminator; One can modify the member properties and methods can only be used in the internal special-handed class; The class that he modifies cannot be inherited, and the method cannot be overridden;

s = = and new and $this have the static of "revenge"; Used with the 3P modifier, usually trailing behind, preceded by a name, and the decorated member property is a common attribute; unique Access form

Inside of class: Self:: External class name for member property/member Method Class:: member property/member method

c = Const Love to play within the class constant definition mode; Format const AB = '; Note: The value of the calculation, callable function, variable "not cold", and do not participate in the process control;

The external use of the internal const class define defines the Access form and the static consistency;

i = instanceof (DNA tester); Used to determine whether an object is instantiated by the class or subclass of the class;

c = Clone clones; Often shared with __clone, automatically executed when copying objects using __clone, to initialize newly created objects (mirror and no value);

A =>__autoload (procedural ape mind); Automatic introduction of files for lazy people;

t = ToString (Magic method) when performing a string operation on an object, as long as the value of the string class is returned, none of the others;

S =>serialize (serialization, serial number); Along with two magic methods __sleep serialization and __wakeup crossdress line;

<!-- Fsci-cats------------------------------------------------------------------------------------------------------------end- -------------->

<!-- AI--------------------------------------------------------------------------------------------------------------------STA RT------------->

A =>abstract abstract (keyword) commonly used 3P after class classes; Features: The class that is modified by the abstract cannot be instantiated directly and the member method has no code body;

Function: It is equivalent to define a fixed template for the subclass, so that it can be used in the inheritance, it must first rewrite all the abstract methods;

The I=>interface (interface) uses the interface keyword to define the interface, only the class constants and abstract methods in the interface, and the inheritance of the interface is different from the other extends implements;

Abstract classes and interfaces of the same point: 1, are not directly instantiated, can have an abstract method, all can be accessed through the class constants, you can define a template for the child class;

Different points: Abstract classes in interfaces do not have to be abstracted, only interface in interfaces, only abstract methods and class constants, interfaces only public;

<!-- Ai---------------------------------------------------------------------------------------------------------------------en D------------->

[Mr. Mak] Common magic methods and keywords in PHP object-oriented

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.