PHP 3.x and 4.x in a constructor about object programming

zhaozj2021-02-11  175

PHP 3.x and 4.x About Object Programming

"Object" sounds a very popular vocabulary, it seems that if you have no OOP, it is better to go home. PHP starts supporting object programming from version 3.x, although its Class has accused from the beginning, it has brought us an unexpected surprise. All the way fell and hit it, and it was 4.X, and PHP was quite OOP. Of course, it is still unsatisfactory for the processing of class variables, no private, public, protected, static statement. The availability of PHP-oriented object is not within the scope of this article. With the perfection of object programming in 4.x, PHP Team brings us "trouble": 3.x and 4.x to some game rules for object programming, not compatible. The author discussed a little discussion on the problems encountered by the actual development process. I believe that some problems may have not been encountered, welcome to supplement, and enjoy.

First, the constructor (function) is said in 3.x, the constructor refers to a class function with the same name as the class. It is a bit embarrassing, but it is even more. In 4.x, the constructor refers to a class function that is directly defined in the constructor inside the constructor, that is, the class function that does not include inheritance. For example: 1,

Class ababystudio {function ababystudio () {echo ('call constructor'.chr (13));} // ...}

Note: The effect is the same in 3.x and 4.x.

Class ababy {function ababystudio () {echo ('ababystudio');}} class ababystudio {// ...}

Note: There is a constructor in 3.x in class ABABYSTUDIO, inherited from the parent class Ababy; there is no constructor in 4.x. Readers can try to run such a line of code $ ababystudio = new ababystudio (); "Ababystudio" should be output in version 3.x, and 4.x will not be output.

It is also worth noting that when an object is instantified in 4.x, it can only specify a constant value (or scalar value) as an initial value for class variables. If you want to pass a variable (usually a global variable), object, the expression is set, you should operate in the constructor. For example: 3,

Class ababy {// ...} Class ababystudio {var $ ababy = 'Hello Every One'; // Sentence 1 var $ ababy = 'hello'. 'world'; //sence 2 var $ ababy = new ababy () ; // seencept 3 var $ ababy = array (); //sence 4 var $ ababy = g_hello; // senence 5, g_hello is a constant function ababystudio () {echo ('call constructor'.chr (13)); } // ...}

Note: 5 marked Sentence only 1, 4, 5 can pass at 4.x. However, the author did not understand why the PHP Workgroup allowed Sentence 4, only forced yourself to understand: Array is used as a basic data type, and Array () is not considered a method or expression. 4,

Class ababy {// ...} class ababystudio {var $ ababy; function ababystudio () {$ this-> ababy = 'Hello Every One'; $ this-> Ababy = 'Hello'. 'Every'. 'One' $ This-> Ababy = new ababy (); $ this-> ababy = array (); $ this-> ababy = g_hello; echo ('call constructor'.chr (13));} // ...}

Note: This code can be compiled in 3.x and 4.x, the effect is the same. In 3.x and 4.x, the description of the change of the constructor rule is stopped here. The next article will introduce the incompatibility problem of the delivery parameters in reference.

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

New Post(0)