Section sixth access properties and methods [6]_php Foundation

Source: Internet
Author: User

A property of an object instance 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. For example, 6.1 prints the line of the user object's Name property.

Can be associated 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 with a string of double references. Looking at the example in 6.5, 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. The call to Getlastlogin in Example 6.1 is. The method executes almost the same as the function outside 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.

<?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 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. Example 6.6 shows how to use the parent namespace to invoke a constructor in the parent class. Also use self to invoke another class method in the constructor.

<?php
Class Animal file://Animal
{
Public $blood; file://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 file://mammals
{
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");
?> The fourth section describes how to call a function. 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.