Can be combined with ->. If an object's properties contain an object, you can use two -> operators to get the properties of the inner object. You can even place these expressions in double-referenced strings. The following example , the property room in the object House contains a set of Room objects.
Access methods are similar to access properties. The -> operator is used to point to the method of the instance. Just call getLastLogin in the following. Methods execute almost the same as functions outside the class.
If a class inherits from another class, the properties and methods in the parent class will be valid in the subclass, even if they are not available in the subclass Declaration. As mentioned before, inheritance is very powerful. If you want to access an inherited property, you only need to reference it as if you accessed the base class's own property, using the :: operator.
Copy code The code is as follows:
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. The following example shows how to use the parent namespace to call the constructor in the parent class. It also uses self to call another class method in the constructor.
Copy code The code is as follows:
class Animal //Animal
{
public $blood; //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 //Mammals
{
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");
?>
The members of the object are called like this: If you need to determine the name of the variable 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 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 implement certain design patterns, such as Factory pattern.
http://www.bkjia.com/PHPjc/318835.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318835.htmlTechArticle can be used in conjunction with ->. If the properties of an object contain an object, you can use two -> operations operator to get the properties of the internal object. You can even use double quoted strings to put...