Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the Properties and methods of PHP5.0 object Model

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "introduction to the properties and methods of the PHP5.0 object model". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to the properties and methods of the PHP5.0 object model".

Today we are going to introduce you to the properties and variables of the PHP5.0 object model, which is the same as other variables, except that you have to use the-> operator to reference them. You do not need to use the dollar sign $before the attribute.

Can be combined with->, if the property of an object contains an object, you can use two-> operators to get the properties of the internal object. You can even place these expressions with double-referenced strings. In the following example, the property room in the object House contains a set of Room objects.

Access methods are similar to access properties. The-> operator is used to point to the method of the instance. Call getLastLogin in the following. 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 it is not declared in the subclass. As mentioned before, inheritance is very powerful. If you want to access an inherited property, you only need to reference it as you would access the base class's own property, using the:: operator.

Class Room {public $name; function _ construct ($name= "unnamed") {$this- > name= $name;}} class House {/ / array (promotional product main product) 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);? >

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. It also uses self to call another class method in the constructor.

Class Animal / / Animal {public $blood; / / warm-blood or Cold Blood attribute 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'woofballs'");}} $d = new Dog ("Black and Tan", "Angus");? >

This is called for members of the object: if you need to determine the name of the variable at run time, you can use an expression such as $this- > $Property. If you want to call the 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 for the PHP5.0 object model like this: $obj- > getObject ()-> callMethod (). This avoids the use of an intermediate variable and helps to implement certain design patterns, such as the Factory pattern.

At this point, I believe you have a deeper understanding of the "introduction to the properties and methods of the PHP5.0 object model". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report