20150110--Magic Method Magic constant + object-oriented-01

Source: Internet
Author: User

Review

Object-Oriented Programming: classes, objects, and instantiation

Class Members: Properties, methods, and class constants, access modifier qualifiers (public,protected and private)

Object access properties and methods: Objects, properties/methods (argument list);

Internal access: $this, representing objects currently entering inside the class

Magic Method: Construction method and destructor method

Object value, object comparison loading for object class

Because to get the object of a class, you must first ensure that the class structure is already in memory (code snippet).

Auto Load

When a class is needed, the system will find the corresponding class file (based on the file name) according to the specified path, and then process (load) the file according to the user-specified action.

Magic function: function that will be called automatically

__autoload (): Auto-load function, all automatic (Magic) is provided a mechanism, can be automatically called by the system, but the internal structure is the user's own definition.

Automatic loading of the usual processing mode

Automatic loading is a processing mechanism (small system) for PHP to load a class and is specifically responsible for loading files.

1. The first step in instantiation: First go to the code snippet to see if the corresponding class exists

2. If it does not exist, the system will check if the automatic loading mechanism (__autoload) is turned on

3. If there is an automatic load, then the system will call the auto-load function, passing in Parameters: class name

Note: The use of automatic loading is the same as the relationship between class files and classes is particularly clear (exactly the same), and the corresponding class file name, must have the rule (class name. class.php) class constant access

A class constant belongs to a class and not to an object. object is not accessible.

Constant access must be achieved through the class:

Class Name:: Constant Name,:: Called Range resolution operator

Because of the frequent use of classes within the class to access the members of the class, the inside of the class is accessed using the class, there is a keyword similar to $this for the object: self, its own meaning, represents the current class name (the individual cannot be printed separately, and the

Memory distribution of constants

The difference between self and $this

1. Self represents class, $this represents object

2. Self accesses constant and static properties and static methods inside the class, $this accessing the object's properties and common methods

3. Self must be in line with the scope resolution operator (::) to take effect, $this represents the object, and the object is inherently a data type, so $this can be printed separately.

4. Where you can use the $this, self is sure to be used, but the place where you can use self is not always available $this

Class constants, static properties, and internal access to static methods use self;

Normal method, property of internal access using $this static

Static is divided into two parts: static properties and Static methods

Static things are for classes (including class constants), non-static are static properties for the object

When defining a property, a property that uses the static modifier of the keyword is called a static property.

A static property belongs to a class that does not belong to an object.

Object statisticians: Counts how many objects the current class is instantiated.

Static methods

Methods that are decorated with the static keyword are called static methods. (static methods are in principle used for classes)

Can solve the problem with static, no ordinary method to solve the problem (static things can directly use the class access, do not need to instantiate through the object, thus saving memory space, improve efficiency).

Object access static method (yes)

The difference between static and non-static methods

Static methods cannot use the $this keyword.

Special attention:

1. In the process of writing code, strictly follow the case specification

2. Strictly follow: Static content to the class access, non-static is to the object access, do not cross-boundary operation Magic Method

object is a composite data type and cannot be output directly using echo.

__tostring (): Refers to a method that is automatically called when an object needs to be converted to a string

The object defaults to a reference pass, so that the object can only be generated by new, but sometimes it needs to be generated by objects, which is a technique that needs to be applied to a clone.

Syntax: variable = clone object variable:

Magic method: __clone (), when cloning an object, the cloned object will go to invoke the method.

Magic Constants

__class__: Represents the current class name and can only be used internally by the class

20150110--Magic Method Magic constant + object-oriented-01

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.