Properties and methods of depth exploration of PHP 5.0 object model

Source: Internet
Author: User
The property of an object instance of an object is a variable, just like any other variable in PHP. But you have to use the-> operator to refer to them. You do not need to use dollar $ $ before attributes.

You can use->, and if an object's properties contain an object, you can get the properties of the inner object using two-> operators. You can even place these expressions with a string of double references. In the following example, the property room in Object house contains a set of room objects.

The access method and the Access property are similar. The method that the-> operator uses to point to an instance. Calling Getlastlogin in the following is. Method executes almost identically to functions outside of the class.

If a class inherits from another category, the properties and methods in the parent class will be valid in the subclass, even if there is no declaration in the child class. As mentioned before, inheritance is very powerful. If you want to access an inherited attribute, you only need to refer to it as if you were accessing the base class's own properties, using the:: 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 room
Print ($home->room[0]->name);
? >
PHP has two special namespaces: the parent namespace points to the superclass, and the self namespace points to the current class. The following example shows how to use the parent namespace to invoke a constructor in the parent class. You also use self to invoke another class method in the constructor.

Class Animal//Animal
{
Public $blood; Blood or cold-blooded properties
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");
? >
This is called for the members of the object: if you need to determine the name of the variable at run time, you can use $this-> to $Property such an expression. If you want to invoke the method, you can use the $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 some design patterns, such as Factory mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.