Home  >  Article  >  Backend Development  >  Accessing properties and methods_PHP Tutorial

Accessing properties and methods_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:24:58788browse

Accessing properties and methods

Properties of an object instance are variables, just like other variables in PHP. But you must use the -> operator to reference them. There is no need to use the dollar sign $ before the property.

can be combined with ->. If the properties of an object contain an object, you can use two -> operators to get the properties of the internal object. You can even These expressions can be placed in double-quoted strings.

Accessing methods is similar to accessing properties. The -> operator is used to point to methods of instances. Methods execute almost the same as functions outside the class.

If a class inherits from another class, the properties and methods in the parent class will be valid in the child class, even if they are not declared in the child class. As mentioned before, inheritance is very powerful . If you want to access an inherited property, you just need to reference it as you would access the base class's own property, 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 first room of the house    print($home->room[0]->name); ?>

PHP has two A special namespace: 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. Self is also used to call the constructor in the constructor. Another class method.

<?php    class Animal //动物    {        public $blood; //热血or冷血属性        public $name;        public function __construct($blood, $name=NULL)        {            $this->blood = $blood;            if($name)            {                $this->name = $name;            }        }    }    class Mammal extends Animal  //哺乳动物    {        public $furColor; //皮毛颜色        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"); ?>

If you need to determine the name of a variable 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 was 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 pattern.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446726.htmlTechArticleAccessing Properties and Methods The properties of an object instance are variables, just like other variables in PHP. But you must use -> operator to reference them. There is no need to use the dollar sign $ before the attribute. You can...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn