PHP5 Object Model [15] - Other features (latest)

zhaozj2021-02-16  138

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 a 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.

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

New Post(0)