/* +-------------------------------------------------- --------------------------------+ | = This article is read by Haohappy> | = Classes and Objects 1 Chapter's notes | = Translation + personal experience | = To avoid unnecessary trouble, please do not reprint, thank you | = Criticisms and corrections are welcome, and I hope to make progress together with all PHP enthusiasts! +------- -------------------------------------------------- -----------------------+ */ Section 6 - Accessing properties and methods The properties of an object instance are variables, just like other variables in PHP. But you must use the -> operator to refer to them. There is no need to use the dollar sign $ before the attribute. For example, the line in 6.1 that prints the name attribute of the User object. You can use -> in conjunction with it if an object's attribute contains an object , you can use the two -> operators to get the properties of the internal object. You can even use double-referenced strings to place these expressions. Looking at the example in 6.5, the property room in the object House contains a set of Room objects Accessing methods is similar to accessing properties. The -> operator is used to point to methods on the instance. This is done by calling getLastLogin in Example 6.1. Methods perform almost the same as functions outside the class. If a class inherits from another class, Properties and methods in the parent class will be available in the child class, even if they are not declared in the child class. As mentioned before, inheritance is very powerful. If you want to access an inherited property, you only need to access it like The base class's own attributes can be referenced like this, using the :: 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("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. Self is also used to call another class method in the constructor. 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 introduces how to call functions. For members of the object, the call is like this: If you need to determine the name of the variable at runtime, you can use $this->$Property like this expression. 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 was not allowed in previous versions of PHP. For example, You can write an expression like this: $obj->getObject()->callMethod(). This avoids the use of an intermediate variable and also helps to implement certain design patterns, such as Factory pattern.