Section 6 - Access Properties and Methods - Classes and Objects in PHP5 [6]

zhaozj2021-02-16  52

Section 6 - Access Properties and Methods - Classes and Objects in PHP5 [6]

Author: Leon Atkinson translation: Haohappy Source: Beyond the PHP / * ------------------------------------ ------------------------------------------ | = This article is HaohappY << CORE PHP Programming >> | = Classes and Objects Chapter Note | = Translate Main Personal Experience | = To avoid unnecessary troubles, please do not reprint, thank you | = Welcome criticism, hope and all PHP enthusiasts progress together! ----------------------------------------- ---------------------------------- * / Section 6 - Access Properties and Methods A object The attributes of the instance are variables, just like the other variables of PHP. But you must use the -> operator to reference them. Do not need to use dollar values ​​before properties. For example, print the User object's Name property in 6.1. You can In->, if an object's property contains an object, you can use two-> operators to get the properties of the internal object. You can even use double reference strings to place these expressions. Look at 6.5 Example, the property Room in the object House contains a set of ROOM objects. Access methods and access properties are similar. -> operators are used to point to the instance. In the example 6.1, the GetLogin is called GetLogin is. Method execution and classifier is almost Same. If a class is inherited from another class, the properties and methods in the parent class will be valid in subclasses, even in subclasses, there is no statement in subclats. As mentioned earlier, inheritance is very powerful. If you want to access a inherited property, you only need to reference the base class yourself, use :: operator. 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 ("Kitch En "); $ home-> room [] = new room (" bath the first room of the house print ($ home-> room [0] -> name);?> PHP has two Special namespace: Parent namespace points to the parent class, 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, use Self to call another in the constructor A class method. 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-> leggs = $ legs;}} Class dog extends Mammal {Function __Construct ($ FURCOLOR, $ Name) {Parent :: __ Construct ($ FURCOLOR, 4, $ Name); Self :: bark ();} function bar () {print ("$ this-> name says 'woo!');}} $ d = new dog (" Black and Tan "," Angus ");?> The four chapters introduced how to call functions. For members of the object, call this: If you need to determine the name of the variable at runtime, you can use $ this -> $ profersty, if you want to call the method, You can use $ OBJ -> $ Method (). You can also use the -> operator to return a value of a function, which is not allowed in the previous version of PHP. For example, you can write a expression like this: $ OBJ-> getObject () -> CallMethod (). This avoids using an intermediate variable, which also helps implement certain design patterns, such as Factory mode. Related resources: Section 6 - Access Properties and Methods

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

New Post(0)