Section sixth accessing properties and methods [6]_php Tutorial

Source: Internet
Author: User
Tags access properties
A property of an object instance is a variable, just like any other variable in PHP. However, you must refer to them using the-I operator. You do not need to use the dollar symbol $ before the property. For example, 6.1 prints the Name property of the user object in the row.

can be associated with-> If an object's properties contain an object, you can use the two-by operator to get the properties of the inner object. You can even place these expressions with double-referenced strings. Looking at the example in 6.5, the property house in the object house contains a set of House objects.

Access methods and access properties are similar. The operator is used to point to the method of the instance. In Example 6.1, the Getlastlogin is called. The method executes almost the same as a function outside the class.

If one class inherits from another, the properties and methods in the parent class will be valid in the subclass, even if there is no declaration in the subclass. As mentioned before, inheritance is very powerful. If you want to access an inherited property, you only need to refer to it as you would access the base class's own property, using the:: operator.

Class
{
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 ("bedroom");
$home->room[] = new ("Kitchen");
$home->room[] = new ("bathroom");

Show the first house
Print ($home->room[0]->name);
?> PHP has two special namespaces: the parent namespace points to the parents class, and the self namespace points to the current class. Example 6.6 shows how to use the parent namespace to invoke constructors in the parent class. Also use self to invoke another class method in the constructor.

Class Animal file://Animals
{
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");
?> in section Fourth describes how to call a function. This is called for members of the object: if you need to determine the name of the variable at run time, you can use $this-> $Property such an expression. If you want to invoke the method, you can use $obj-> $method ().

You can also use the-I operator to return the value of a function, which is not allowed in previous versions of PHP. For example, you could 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 the factory pattern.

http://www.bkjia.com/PHPjc/314117.html www.bkjia.com true http://www.bkjia.com/PHPjc/314117.html techarticle 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 the dollar symbol $ before the property. For example, 6.1 prints the user to ...

  • 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.