*
+-------------------------------------------------------------------------------+
| = This article is read by Haohappy<<Core PHP Programming>>
| =Notes from the chapter Classes and Objects
| = Translation as the main + personal experience
| = To avoid unnecessary troubles that may occur, please do not reprint, thank you
| = Criticism and correction are welcome, and I hope to make progress with all PHP enthusiasts!
| = PHP5 Research Center:/haohappy2004
+-------------------------------------------------------------------------------+
*/
Section 6 - Accessing properties and methods
The properties of an object instance are variables, just like other variables in PHP. But you have to use the -> operator to reference them. There is no need to use the dollar sign $ before the attribute. For example, print the line of the name attribute of the User object in 6.1.
You can use -> in conjunction with ->. If an object's attribute contains an object, you can use two -> operators to get the attributes of the internal object. You can even use double-referenced strings to place these expressions. See the example in 6.5. The property room in the object house contains a set of Room objects.
Access methods are similar to access properties. The method used by the -> operator to point to an instance. Calling getLastLogin in Example 6.1 is to execute the method almost the same as a function 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 declared in the subclass. As mentioned before, inheritance is very powerful. If you want to access an inherited property, you just need to reference it like you can access the base class's own properties, using the :: operator.
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, self is also used to call another class method in the constructor.
Chapter 4 introduces how to call functions. This is how to call objects members: If you need to determine the name of the variable at runtime, you can use expressions such as $this->$Property. If you want to call methods, 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 to implement certain design patterns, such as Factory patterns.
+-------------------------------------------------------------------------------+
| = This article is read by Haohappy<<Core PHP Programming>>
| =Notes from the chapter Classes and Objects
| = Translation as the main + personal experience
| = To avoid unnecessary troubles that may occur, please do not reprint, thank you
| = Criticism and correction are welcome, and I hope to make progress with all PHP enthusiasts!
| = PHP5 Research Center:/haohappy2004
+-------------------------------------------------------------------------------+
*/
Section 6 - Accessing properties and methods
The properties of an object instance are variables, just like other variables in PHP. But you have to use the -> operator to reference them. There is no need to use the dollar sign $ before the attribute. For example, print the line of the name attribute of the User object in 6.1.
You can use -> in conjunction with ->. If an object's attribute contains an object, you can use two -> operators to get the attributes of the internal object. You can even use double-referenced strings to place these expressions. See the example in 6.5. The property room in the object house contains a set of Room objects.
Access methods are similar to access properties. The method used by the -> operator to point to an instance. Calling getLastLogin in Example 6.1 is to execute the method almost the same as a function 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 declared in the subclass. As mentioned before, inheritance is very powerful. If you want to access an inherited property, you just need to reference it like you can access the base class's own properties, using the :: operator.
Copy the codeThe code is as follows:
<?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);
?>
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, self is also used to call another class method in the constructor.
Copy the codeThe code is as follows:
<?php
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 //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");
?>
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 //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. This is how to call objects members: If you need to determine the name of the variable at runtime, you can use expressions such as $this->$Property. If you want to call methods, 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 to implement certain design patterns, such as Factory patterns.