English 中文(简体)
作为对象的方法 - 如何/当[重复]
原标题:Methods as objects - How / when [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
PHP method chaining?

在我合作过的很多广告中, 我见过这样的事情:

$object->method()->anotherMethod();

我读过OOP的教程,这就是课程的写法,

<?php
class myClass {
    public method() {
       // do something
    }
}
?>

对不起,但我对OOP是新手。

最佳回答

如果您的方法返回 $Thi, 您可以使用上述样式 ($object- & gt; & getod ()- & gt; aotherMethod () ) ) 。 只有在您的方法无法返回其它东西的情况下才能这样做, 例如, 名为 get something () 的方法预计将返回 anything , 但如果您的方法没有相关值可以返回, 您可以只返回 $This, 允许使用方法调用链 。

问题回答

这叫做" http://en.wikipedia.org/wiki/Method_lagening" rel=“nofollow”>Method呼叫绑链 。对于你何时应该使用它,没有硬规则或快速规则,但我使用的一般规则是,当一系列物体方法经常被称作一个接一个时,方法链条就有意义,例如初始化功能。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...