Home php教程 php手册 PHP5.0对象模型的属性和方法分析

PHP5.0对象模型的属性和方法分析

Jun 13, 2016 am 11:05 AM
introduce about analyze Can and right object Attributes us method yes Model of

今天我们向大家介绍的是关于可以联用->,如果一个对象的属性包含了一个对象,你可以使用两个->运算符来得到内部对象的属性。 你甚至可以用双重引用的字符串来放置这些表达式。 下面的例子中,对象House中的属性room包含了一组Room对象。

访问方法和访问属性类似。->运算符用来指向实例的方法。 在下面的中调用getLastLogin就是。方法执行起来和类外的函数几乎相同。

如果一个类从另一类中继承而来,父类中的属性和方法将在子类中都有效,即使在子类中没有声明。 像以前提到过的,继承是非常强大的。 如果你想访问一个继承的属性,你只需要像访问基类自己的属性那样引用即可,使用::运算符。

<ol class="dp-xml">
<li class="alt"><span><span>class Room   </span></span></li>
<li class=""><span>{   </span></li>
<li class="alt"><span> public $name;   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span> function __construct($</span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"unnamed"</font></span><span>)   </span>
</li>
<li class=""><span> {   </span></li>
<li class="alt">
<span>$this-></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $name;   </span>
</li>
<li class=""><span> }   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>class House   </span></li>
<li class=""><span>{   </span></li>
<li class="alt"><span> //array(促销产品 主营产品) of rooms   </span></li>
<li class=""><span> public $room;   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//create empty house   </span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">home</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> house;   </span>
</li>
<li class="alt"><span> </span></li>
<li class=""><span>//add some rooms   </span></li>
<li class="alt"><span>$home->room[] = new Room("bedroom");   </span></li>
<li class=""><span>$home->room[] = new Room("kitchen");   </span></li>
<li class="alt"><span>$home->room[] = new Room("bathroom");   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//show the first room of the house   </span></li>
<li class=""><span>print($home->room[0]->name);   </span></li>
<li class="alt"><span>?>  </span></li>
</ol>
Copy after login


PHP5.0对象模型有两个特殊的命名空间:parent命名空间指向父类,self命名空间指向当前的类。下面的例子中显示了如何用parent命名空间来调用父类中的构造函数。 同时也用self来在构造函数中调用另一个类方法。

<ol class="dp-xml">
<li class="alt"><span><span>class Animal //动物   </span></span></li>
<li class=""><span>{   </span></li>
<li class="alt"><span> public $blood; //热血or冷血属性   </span></li>
<li class=""><span> public $name;   </span></li>
<li class="alt">
<span> public function __construct($blood, $</span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">NULL</font></span><span>)   </span>
</li>
<li class=""><span> {   </span></li>
<li class="alt">
<span>$this-></span><span class="attribute"><font color="#ff0000">blood</font></span><span> = $blood;   </span>
</li>
<li class=""><span>if($name)   </span></li>
<li class="alt"><span>{   </span></li>
<li class="">
<span> $this-></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $name;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li class=""><span> }   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>class Mammal extends Animal //哺乳动物   </span></li>
<li class=""><span>{   </span></li>
<li class="alt"><span> public $furColor; //皮毛颜色   </span></li>
<li class=""><span> public $legs;   </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span> function __construct($furColor, $legs, $</span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">NULL</font></span><span>)   </span>
</li>
<li class="alt"><span> {   </span></li>
<li class=""><span>parent::__construct("warm", $name);   </span></li>
<li class="alt">
<span>$this-></span><span class="attribute"><font color="#ff0000">furColor</font></span><span> = $furColor;   </span>
</li>
<li class="">
<span>$this-></span><span class="attribute"><font color="#ff0000">legs</font></span><span> = $legs;   </span>
</li>
<li class="alt"><span> }   </span></li>
<li class=""><span>}   </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>class Dog extends Mammal   </span></li>
<li class="alt"><span>{   </span></li>
<li class=""><span> function __construct($furColor, $name)   </span></li>
<li class="alt"><span> {   </span></li>
<li class=""><span>parent::__construct($furColor, 4, $name);   </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>self::bark();   </span></li>
<li class="alt"><span> }   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span> function bark()   </span></li>
<li class=""><span> {   </span></li>
<li class="alt"><span>print("$this->name says 'woof!'");   </span></li>
<li class=""><span> }   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">d</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> Dog("Black and Tan", "Angus");   </span>
</li>
<li class=""><span>?>  </span></li>
</ol>
Copy after login

对于对象的成员来是这样调用的:如果你需要在运行时确定变量的名称,你可以用$this->$Property这样的表达式。 如果你想调用方法,可以用$obj->$method()。

你也可以用->运算符来返回一个函数的值,这在PHP以前的版本中是不允许的。例如,你可以写一个像这样的PHP5.0对象模型的表达式: $obj->getObject()->callMethod()。这样避免了使用一个中间变量,也有助于实现某些设计模式,如Factory模式。


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

The world's most powerful open source MoE model is here, with Chinese capabilities comparable to GPT-4, and the price is only nearly one percent of GPT-4-Turbo The world's most powerful open source MoE model is here, with Chinese capabilities comparable to GPT-4, and the price is only nearly one percent of GPT-4-Turbo May 07, 2024 pm 04:13 PM

Imagine an artificial intelligence model that not only has the ability to surpass traditional computing, but also achieves more efficient performance at a lower cost. This is not science fiction, DeepSeek-V2[1], the world’s most powerful open source MoE model is here. DeepSeek-V2 is a powerful mixture of experts (MoE) language model with the characteristics of economical training and efficient inference. It consists of 236B parameters, 21B of which are used to activate each marker. Compared with DeepSeek67B, DeepSeek-V2 has stronger performance, while saving 42.5% of training costs, reducing KV cache by 93.3%, and increasing the maximum generation throughput to 5.76 times. DeepSeek is a company exploring general artificial intelligence

KAN, which replaces MLP, has been extended to convolution by open source projects KAN, which replaces MLP, has been extended to convolution by open source projects Jun 01, 2024 pm 10:03 PM

Earlier this month, researchers from MIT and other institutions proposed a very promising alternative to MLP - KAN. KAN outperforms MLP in terms of accuracy and interpretability. And it can outperform MLP running with a larger number of parameters with a very small number of parameters. For example, the authors stated that they used KAN to reproduce DeepMind's results with a smaller network and a higher degree of automation. Specifically, DeepMind's MLP has about 300,000 parameters, while KAN only has about 200 parameters. KAN has a strong mathematical foundation like MLP. MLP is based on the universal approximation theorem, while KAN is based on the Kolmogorov-Arnold representation theorem. As shown in the figure below, KAN has

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

FisheyeDetNet: the first target detection algorithm based on fisheye camera FisheyeDetNet: the first target detection algorithm based on fisheye camera Apr 26, 2024 am 11:37 AM

Target detection is a relatively mature problem in autonomous driving systems, among which pedestrian detection is one of the earliest algorithms to be deployed. Very comprehensive research has been carried out in most papers. However, distance perception using fisheye cameras for surround view is relatively less studied. Due to large radial distortion, standard bounding box representation is difficult to implement in fisheye cameras. To alleviate the above description, we explore extended bounding box, ellipse, and general polygon designs into polar/angular representations and define an instance segmentation mIOU metric to analyze these representations. The proposed model fisheyeDetNet with polygonal shape outperforms other models and simultaneously achieves 49.5% mAP on the Valeo fisheye camera dataset for autonomous driving

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Apr 29, 2024 pm 04:55 PM

FP8 and lower floating point quantification precision are no longer the "patent" of H100! Lao Huang wanted everyone to use INT8/INT4, and the Microsoft DeepSpeed ​​team started running FP6 on A100 without official support from NVIDIA. Test results show that the new method TC-FPx's FP6 quantization on A100 is close to or occasionally faster than INT4, and has higher accuracy than the latter. On top of this, there is also end-to-end large model support, which has been open sourced and integrated into deep learning inference frameworks such as DeepSpeed. This result also has an immediate effect on accelerating large models - under this framework, using a single card to run Llama, the throughput is 2.65 times higher than that of dual cards. one

See all articles