/* + ------------------------------------------------------------------------------- + | = Read this article for haohappy <core PHP programming> | = Notes in the classes and objects chapter | = Translation-oriented + personal experiences | = Do not repost it to avoid unnecessary troubles. Thank you. | = Thank you for your criticism and hope to make progress together with all PHP fans! + ------------------------------------------------------------------------------- + */Section 6 -- access attributes and Methods The attribute of an object instance is a variable, just like other variables in PHP. however, you must use the-> operator to reference them. $. for example, in 6.1, print the row of the name attribute of the user object. You can use this function together. If an object property contains an object, you can use two-> operators to obtain the attributes of an internal object. you can even use double-referenced strings to place these expressions. see the example in section 6.5. The property room in the object house contains a group of room objects. The access method is similar to the access attribute.-> operator is used to point to the instance method. In example 6.1, calling getlastlogin is. The method is executed almost the same as the function outside the class. If a class is inherited from another class, the attributes and methods in the parent class are valid in the subclass even if they are not declared in the subclass. as mentioned before, inheritance is very powerful. if you want to access an inherited attribute, you only need to reference it like accessing the attributes of the base class. Use the: operator. <? PHP Class room { Public $ name; Function _ construct ($ name = "unnamed ") { $ This-> name = $ name; } } Class house { // Array of rooms Public $ room; } // Create empty house $ Home = new house; // Add some rooms $ Home-> room [] = new room ("bedroom "); $ Home-> room [] = new room ("Kitchen "); $ Home-> room [] = new room ("bathroom "); // Show the first room of the house Print ($ home-> room [0]-> name ); ?> PHP has two special namespaces: the parent namespace points to the parent class, And the self namespace points to the current class. example 6.6 shows how to use the parent namespace to call the constructor in the parent class. at the same time, we also use self to call another class method in the constructor. <? PHP Class animal // animal { Public $ blood; // attribute of hot blood or cold blood Public $ name; Public Function _ construct ($ blood, $ name = NULL) { $ This-> blood = $ blood; If ($ name) { $ This-> name = $ name; } } } Class mammal extends animal // mammal { Public $ furcolor; // fur color Public $ legs; Function _ construct ($ furcolor, $ legs, $ name = NULL) { Parent: :__ construct ("warm", $ name ); $ This-> furcolor = $ furcolor; $ This-> legs = $ legs; } } Class dog extends mammal { Function _ construct ($ furcolor, $ name) { Parent: :__ construct ($ furcolor, 4, $ name ); SELF: bark (); } Function bark () { Print ("$ this-> name says 'Woof! '"); } } $ D = new dog ("black and tan", "Angus "); ?> Chapter 4 describes how to call a function. the object is called as follows: If you need to determine the variable name at runtime, you can use an expression like $ this-> $ property. if you want to call a method, you can use $ obj-> $ method (). You can also use the-> operator to return the value of a function, which is not allowed in previous PHP versions. for example, you can write an expression like this: $ obj-> GetObject ()-> callmethod (). this avoids the use of an intermediate variable and helps implement some design patterns, such as the factory mode. |