PHP5 object system

zhaozj2021-02-16  149

PHP5 object system

Haohappy @ phpe / text

* This article addresses the overall framework of the PHP5 object system for the "Classes and Objects IN PHP5" series of articles, but some features are not specified. It is highly recommended to read this article after reading "Classes and Objects in PHP5".

The object system launched by PHP5 is believed to be the most expected. PHP5 draws on the Java2 object model, providing more powerful object-oriented programming, using PHP to achieve OO will become easier and natural.

Object

PHP5 uses the Zend Engine II, and the object is stored in an independent structure Object Store, and is not stored in ZVAL like other general variables (stored in ZVAL as the object and general variables in PHP4). The pointer to the object is stored in ZVAL instead of content (value). We do not need to copy data when we copy an object or pass an object as a parameter to a function. Only the same object pointer and notification from another ZVAL now point to the Object Store now. Since the object itself is located in the Object Store, any change we do will affect all ZVAL structures held by the object pointer --- Manifestation is that any change in the target object will affect the source object. This makes the PHP object seem to be passed by reference (Reference), so the objects in PHP defaults to pass through "reference", and you no longer need to use & to declare it in PHP4.

Garbage collection mechanism

Some languages, most typical C, need you explicitly request allocation to allocate inscribed portability when you create a data structure. Once you assign to memory, you can store information in variables. At the same time, you also need to release the memory while ending using variables, which allows the machine to empty to other variables to avoid lighting memory.

PHP can automatically perform memory management, clear the object that is no longer needed. PHP uses a reference count (Reference Counting) mechanism. Each object contains a reference counter, and each Reference is connected to the object, and the counter adds 1. When Reference is left to the living space or set to NULL, the counter minus 1. When a reference counter of an object is zero, PHP knows that you will no longer need to use this object, release the memory space it occupied.

E.g:

The above is the change in php5 in memory management, maybe everyone is not interested. Let's take a look at the object model in PHP5 and what specific differences in PHP4:

★ New function

★ Improvement function

1) ★ Private and protected membels private and protected members (attributes, methods)

2) ★ Abstract Classes and methods Abstract and Abstract Method

3) ★ Interfaces interface

4) ★ Class Type Hints Type Indicator =

5) ★ Final Final Keyword =

6) ★ Objects cloning object replication =

7) ★ Constructors and destructors constructor and destructors

8) ★ Class Constants are currently =

9) ★ Exceptions exception handling

10) ★ Static MEMBER static class member

11) ★ __method__ constant __method__ constant =

12) ★ Reflection reflex mechanism

The first, 2, 3, 7, 10, please refer to the "Classes and Objects In PHP5" series at the end of this article, which has been described in detail, which is no longer explained herein. The 9th point abnormal treatment and the 12th point reflex mechanism are more rich, limited to the paradodes, please pay attention to the second phase of the "PHP & More" Electronics Magazine, will be introduced to the article.

Here you introduce you to the 4th, 5th, 6th, 8th, 11 language characteristics:

4) ★ Class Type Hints type indication

Everyone knows that PHP is a weak type of language. Do not need to be defined before using variables, no data type of the variable is required. This brings a lot of convenience in programming, but also has some hidden dangers, especially when the variable is changed. In PHP5 adds a type indication, you can automatically determine the parameter type of the class method during the execution process. This is similar to RTTI in Java2, with reflection, allow us to control objects well.

a ($ b); $ A-> b ($ b);?>>

In a strong type language, the type of all variables will be checked at compile, and the type instructions in PHP are used to check the type of checks. If the type of class method parameters is wrong, an error message similar to "Fatal Error: Argument 1 Must IMPLEMENT INTERFACE BAR ..." will be reported.

The following code:

Equivalent to:

5) ★ Final Final Keyword

PHP5 newly added Final keywords, which can be added before class or class method. The class method identified as final, and cannot be overwritten in the subclass. The class identified as final cannot be inherited, and the method is default that the final type. Final method:

Final class:

6) ★ Objects cloning object replication

The previous memory management part said that the object is passed by the reference in PHP5. Objects represented by methods using $ Object2 = $ Object1 are interrelated. If we do need to copy a value that is the same object, it is desirable that the target object is not associated with the source object (like a regular variable to pass the value), then the Clone keyword needs to be used. If you also want to change some of the sources in the source object while copying, you can set a __clone () function, join the operation.

id = self: $ ID ;} / * function __clone () {$ this-> address = "new York "$ This-> id = Self :: $ ID ;} * /} $ obj = new mycloneable (); $ obj-> name =" hello "; $ obj-> address =" tel-aviv "; print $ Obj-> ID. "/ n"; $ obj_cloned = clone $ OBJ; Print $ OBJ_CLONED-> ID. "/ n"; Print $ OBJ_CLONED-> name. "/ n"; Print $ OBJ_CLONED-> Address. "/ n ";?>

The above code copies an exact identical object.

Then remove the comment of the function of function __clone (), re-run the program. A object that is substantially identical, but partial property changes.

8) ★ Class Constants

CONST keywords can be used in PHP5 to define class constants.

11) ★ __Method__ constant __method__ constant

__Method__ is the new "magic" constant in php5, indicating the name of the class method.

Magic constants is a PHP predefined constant, and its value can be changed. Other existing magic constants in PHP have __line __, __ file __, __ function __, __ class__, etc..

Reference:

1. PHP official manual in PHP5 related sections:

http://www.php.net/manual/en/language.oop5.php

http://www.php.net/manual/en/migration5.oop.php

Unfortunately, there are still many chapters in the official manual.

2. "Classes and Objects In PHP5" series of articles, currently seems to be the only Chinese data for the object model in PHP5 in the system.

special reminder:

Some of them (canceled in the official version of the PHP), such as the new CLONE keyword to call __clone () (Section 5); Cancel Namespace Namespace (Section 14). If you have doubts, everything is subject to the official manual, and use PHP

5.0.0

Official version of self-test sample code.

转载请注明原文地址:https://www.9cbs.com/read-8652.html

New Post(0)