The properties of an object instance are variables, just like other variables in PHP. But you must use the -> operator to reference them. There is no need to use the dollar sign $ before the property. For example, when printing the name property of the User object in 6.1 One line.
can be used with ->. If the properties of an object contain an object, you can use two -> operators to get the properties of the inner object. You can even use double reference characters 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. The
access method is similar to accessing the attribute. The -> operator is used to point to the method of the instance. . Calling getLastLogin in Example 6.1 is. The method execution is almost the same as the function outside the class.
If a class inherits from another class, the properties and methods in the parent class will be in the subclass are valid, even if not declared in the subclass. As mentioned before, inheritance is very powerful. If you want to access an inherited property, you only need to reference it as you would access the base class's own property, using: :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;
//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 in the constructor. Call another class method in the function.
class Animal file://animal
{
public $blood; file://hot-blooded or cold-blooded attribute
public $name;
public function __construct($blood, $name=NULL)
{
$this->blood = $blood;
if($name)
{
$this->name = $name;
}
}
}
class Mammal extends Animal file://mammal
{
public $furColor; file://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");
?>
Section 4 introduces how to call functions. For members of objects, this is how to call: If you need to determine the name of a variable at runtime, you can use an expression like $this->$Property. If you want to call 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 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 implement certain design patterns, such as Factory pattern.