解决php无限递归问题
我目前遇到了无休止的递归情况。
我正在实现一个调用各种对象方法的消息服务..它与观察者模式非常相似..
这是发生的事情:
Dispatcher.php
class Dispatcher {
...
public function message($name, $method) {
// Find the object based on the name
$object = $this->findObjectByName($name);
if(!$object->decorations))
// Decorate the object
$object = new $name($object); // This is where it locks up.
$object->decorations = true;
}
return $object->$method();
...
}
class A {
function __construct()
{
$Dispatcher->message("B", "getName");
}
public function getName() {
return "Class A";
}
}
class B {
function __construct()
{
// Assume $Dispatcher is the classes
$Dispatcher->message("A", "getName");
}
public function getName() {
return "Class B";
}
}
当两个对象都没有初始化时它会锁定。它只是互相来回传递消息,没有人可以初始化。
我正在寻找某种队列实现,它将使消息相互等待。返回值仍然被设置。我希望 A 类和 B 类中的样板代码尽可能少。
我收到了很多关于 not_initialized
方法的讨论,不幸的是我觉得讨论的方向是错误的。这是我的错,我会进一步解释一下情况(我希望能解释得更好)。
$object
实现了装饰器模式 - 也就是说,当我初始化类 A 和类 B 时,$object
获得更多功能(方法)。我需要使用从消息传递方法中的装饰,这就是为什么如果尚未装饰,则装饰它。我真的不认为 not_initialized
会成为问题,因为当检查 $objectA
时,它尚未装饰 - 所以我想向其中添加这些方法。 A类和B类是装饰器类。所以 not_initialized 当时是正确的。当我尝试装饰该对象时它被卡住了。我更改了代码以更好地反映这种情况。
I'm currently running into an endless recursion situation.
I'm implementing a message service that calls various object methods.. it's quite similar to observer pattern..
Here's whats going on:
Dispatcher.php
class Dispatcher {
...
public function message($name, $method) {
// Find the object based on the name
$object = $this->findObjectByName($name);
if(!$object->decorations))
// Decorate the object
$object = new $name($object); // This is where it locks up.
$object->decorations = true;
}
return $object->$method();
...
}
class A {
function __construct()
{
$Dispatcher->message("B", "getName");
}
public function getName() {
return "Class A";
}
}
class B {
function __construct()
{
// Assume $Dispatcher is the classes
$Dispatcher->message("A", "getName");
}
public function getName() {
return "Class B";
}
}
It locks up when neither object is initialized. It just goes back and forth from message each other and no one can be initialized.
I'm looking for some kind of queue implementation that will make messages wait for each other.. One where the return values still get set. I'm looking to have as little boilerplate code in class A and class B as possible.
I'm getting a lot of talk about the not_initialized
method, unfortunately I feel that the discussion is going in the wrong direction. That's my fault, I'll explain the situation a little more (and better I hope).
$object
implements a decorator pattern - that is $object
gets more features (methods) when I initialize class A and class B. I need to use those features made available from the decorations in the messaging method, that's why if its not yet decorated, decorate it. I really do not think not_initialized
would have been the issue because when say $objectA
is checked it is not yet decorated - So I want to add those methods to it. Class A and B are decorator classes. So not_initialized is correct at the time. It gets stuck when I try to decorate the object. I changed the code to better reflect this situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是死锁的例子,而是无限递归的例子。在构造函数完全运行之前,您的
not_initialized
函数很可能会返回 true。结果,构造 A 类型的对象将间接调用 B 构造函数,B 构造函数又会无限调用 A 构造函数。
您必须更改
not_initialized
函数,或者如果可以的话,将消息分派移出构造函数。死锁是一种涉及多个进程的情况。在这种情况下,只有一个进程。
This is not an example of deadlock, but of endless recursion. Most likely your
not_initialized
function returns true until the constructor has been fully run.As a result, constructing an object of type A will indirectly call the B constructor, which will call the A constructor, ad infinitum.
You will have to change the
not_initialized
function, or move the message dispatching out of the constructors if you can.Deadlock is a situation that involves multiple processes. In this case, there is only a single process.