Object Model of PHP5 [6] - Access Properties and Methods

zhaozj2021-02-16  115

Section 6 - Access Properties and Method An object instance is a variable, just like PHP's other variables. But you must use -> operators to reference them. Do not need to use dollar values ​​before properties. For example, 6.1 Print the Name property of the User object. You can use->, if an object's properties contain an object, you can use two-> operators to get the internal object properties. You can even use double reference characters Place these expressions. Look at the example in 6.5, the property Room in the object House contains a set of Room objects. Access methods and access properties are similar. -> operators to point to the instance. In Example 6.1 Call GetLastLogin That is. Method is executed and the class is almost the same. If a class is inherited from another class, the properties and methods in the parent class will be effective in subclasses, even in subclasses. Prerequisitated, inheritance is very powerful. If you want to access a inherited property, you only need to reference the base class to the property yourself, use :: operator.

Class Room {public $ name; function __construct ($ name = "unnamed") {$ this-> name = $ name;}} class house {// array of rooms public $ room;} // create Empty House $ home = New house-> 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: 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 construct Another class method is called in the function.

Class Animal // Animal {Public $ BLOOD; / / Blood OR Cold Properties 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 :: bar ();} function bar () {print ("$ this-> name says 'woo!');}} $ d = new dog (" Black and Tan " , "Angus");?>

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

New Post(0)