Today we will introduce youYou can use the hyphen (-)-> operator to obtain the attributes of an internal object if an object's attributes contain an object. You can even use double-referenced strings to place these expressions. In the following example, the property room in the object House contains a group of Room objects.
The access method is similar to the access attribute. -> An operator is used to point to an instance. In the following example, getLastLogin is called. Methods are executed almost the same as functions 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, using the: operator.
- Class Room
- {
- Public $ name;
-
- Function _ construct ($Name="Unnamed")
- {
- $ This->Name= $ Name;
- }
- }
-
- Class House
- {
- // Array (main product of the promotion product) of rooms
- Public $ room;
- }
-
- // Create empty house
- $Home=NewHouse;
-
- // 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 );
- ?>
The PHP5.0 object model 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. At the same time, we also use self to call another class method in the constructor.
- Class Animal // Animal
- {
- Public $ blood; // attribute of hot blood or cold blood
- 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=NewDog ("Black and Tan", "Angus ");
- ?>
The object is called as follows: 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 a PHP5.0 object model 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.