有人编写了以下 php4 代码,我现在正尝试将其移植到 php5:
该类是 Foo(名称已更改以保护有罪者)。 在其中一种方法中,我们将其称为 save(),该类显然是这样重置的:
$this = new Foo($this->Foo_Id);
这会导致以下错误:
( ! ) Fatal error: Cannot re-assign $this in ...
我的想法是像这样修复它,但我担心它可能不一样:
$this->Foo($this->Foo_Id);
当我包含该类时,PHP 不再抛出解析/ fatal error ,但就像我说的,我会实现与 php4 构造相同的事情吗?
最佳答案
如果不了解类(class)组织的上下文,就很难告诉您正确的解决方案。
根据情况,您可以执行以下操作:
return new Foo($this->Foo_Id);
调用“save”方法的代码将获取 Foo 的实例。从外面看,这可能看起来像:
之前:
$instance->save($ID);
$instance->methodFromClassFoo(); # Instance would be now of class foo. (which btw. is really bad design.
之后:
$foo = $instance->save($ID);
$foo->methodFromClassFoo();
甚至:
$instance = $instance->save($ID); // Instance is now instance of foo.
$instance->methodFromClassFoo();
也许这对你有帮助。
关于关于如何重置类的 php 4 到 5 移植问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238354/