简体   繁体   English

无论如何都要调用OOP方法的顺序?

[英]Is there anyway to get the order of the OOP method being called?

For example lets say I have a set of classes and methods to do so: 例如,假设我有一组类和方法来执行此操作:

$obj->method1()->method2();

Is there anyway for method1() to know with in itself that its the first method being called or for method2 to know that its the last? 反正是方法1()知道它本身是第一个被调用的方法还是方法2知道它的最后一个?

Some more details 更多细节
I just want to be able to build a set of these calls so that it either returns an instance of itself if the call to the method isnt at the end of the chain or return something different if its at the end. 我只是希望能够构建一组这些调用,以便如果对方法的调用不是在链的末尾,则返回自身的实例,或者如果它在结尾时返回不同的东西。

For example 例如

$obj->method1()->method2(); #Here method 2 will return lets say a string.
$obj->method1()->method2()->method3(); #Since method2 isnt at the end of the chain, it should return an instance of itself (or another object) here so that the chain could continue.

EDIT: anyone whoz trying to do this - it is a bad design pattern. 编辑:任何人试图这样做 - 这是一个糟糕的设计模式。

This does seem to be a duplicate. 这似乎是重复的。 Refer to this question for more answers. 有关更多答案,请参阅此问题

Not out of the box, not even with a Stack trace. 没有开箱即用,甚至没有堆栈跟踪。

I guess you could put something together using constants or global variables: 我想你可以使用常量或全局变量将某些东西组合在一起:

Don't try this at home! 不要在家尝试这个!

$GLOBALS["chain"] = array();
$obj->method1()->method2();    // method1 adds member to $GLOBALS["chain"], 
                               // array_push($GLOBALS["chain"], __FUNCTION__);
                               // method2 does the same...
print_r($GLOBALS["chain"]);

That would give you the full chain - not yet which one is the last one, to do that, you would have to pass a flag to method2() . 这会给你一个完整的链 - 不是哪一个是最后一个,要做到这一点,你必须将一个标志传递给method2()

But it would be horribly hacky and pollute your code. 但它会非常hacky并污染你的代码。

Is there a specific reason you need this for? 您需要这个特定原因吗?

I don't think this is possible, no -- at least, I've never seen anything about this. 我不认为这是可能的,不 - 至少,我从来没有见过这个。

(Out of curiosity : why would you need that ? Maybe it would be possible to use onther solution to solve your actual problem ?) (出于好奇:为什么你需要那个?也许有可能使用其他解决方案来解决你的实际问题?)

The only way this is possible is to let the methods save some global state. 唯一可行的方法是让方法保存一些全局状态。

If both methods are in the same class, you could add a variable to the class and let each class set a unique value. 如果两个方法都在同一个类中,则可以向类中添加一个变量,并让每个类设置一个唯一值。

But the question is if this is desirable. 但问题是这是否可取。 This kind of behavior is often not very smart in the long run. 从长远来看,这种行为往往不是很聪明。

All you could do is find out which methods have been called so far , by setting some kind of global state in the class. 您所能做的就是通过在类中设置某种全局状态来找出到目前为止已调用的方法。 But you can't find out what methods are being called after a method, and you wouldn't be able to tell the difference between methods in one chain and methods in another: 但是你无法找出方法后调用的方法,你无法区分一个链中的方法和另一个链中的方法之间的区别:

$obj->m1()->m2();
$obj->m3(); // You would think that m1() and m2() came before this in the same chain

You would need to have a method at the end of each chain to clear the global state in the class. 您需要在每个链的末尾都有一个方法来清除类中的全局状态。

Since it seems you need to see which method comes next in a chain, this won't work for you. 由于您似乎需要查看链中的一个方法,这对您不起作用。

I would say that this is a really bad design pattern, at least for PHP (and every other language I've worked in). 我会说这是一个非常糟糕的设计模式,至少对于PHP(以及我曾经使用的其他语言)。 Each method should do one thing only. 每种方法都应该只做一件事。 If you need a method to either return a string or an object depending on what you need it for later, you are doing something wrong. 如果你需要一个方法来返回一个字符串或一个对象,这取决于你以后需要它,你做错了什么。

Granted, I have done something like this before. 当然,我之前做过类似的事情。 It was a meta-information class for images submitted by users -- you could set it up like this: 它是用户提交的图像的元信息类 - 您可以像这样设置它:

$meta = new ImageMeta();
$meta->first_name("foo")->last_name("bar")->email("baz")->id("guid");

But, if you did this: 但是,如果你这样做:

$meta->first_name();

it would return a string. 它会返回一个字符串。 The default value for the first parameter was NULL, and if the method got NULL, it returned a string. 第一个参数的默认值为NULL,如果方法为NULL,则返回一个字符串。 Otherwise it set (and escaped) an internal value and returned $this . 否则设置(并转义)内部值并返回$this

At first I thought it was kind of cool, but it turned out to be a mistake. 起初我觉得它很酷,但事实证明这是一个错误。 I hate using that class now. 我现在讨厌使用那门课。 Just make one method/function do one thing only and you will be much happier. 只需让一个方法/功能只做件事你就会更开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM