Access attributes and methods. Access attributes and methods the attributes of an object instance are variables, just like other variables in PHP. however, you must use the-operator to reference them. $. yes
Access attributes and methods
The attribute of an object instance is a variable, just like other variables in PHP. but you must use the-> operator to reference them. you do not need to use the $.
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.
The access method and access attribute are similar.-> operator is used to point to the method of the instance. 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; // Hot blood or cold blood attribute 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 ");?> |
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.
The attribute of an object instance is a variable, just like other variables in PHP. however, you must use the-> operator to reference them. $. yes...