关于如何重置类的 php 4 到 5 移植问题
有人编写了以下 php4 代码,我现在正尝试将其移植到 php5:
该类是 Foo(名称已更改以保护有罪者)。 在其中一种方法中,我们将其称为 save() 类显然是这样重置的:
$this = new Foo($this->Foo_Id);
这会导致以下错误:
( ! ) Fatal error: Cannot re-assign $this in ...
我的想法是像这样修复它,但我担心它可能不一样:
$this->Foo($this->Foo_Id);
当我包含该类时,PHP 不再抛出解析/致命错误,但就像我说的,我会实现与 php4 构造相同的事情吗?
Someone wrote the following php4 code which I now am trying to port to php5:
the class is Foo (name changed to protect the guilty).
In one of the methods, we'll call it save() the class apparently is reset like this:
$this = new Foo($this->Foo_Id);
That results in the following error:
( ! ) Fatal error: Cannot re-assign $this in ...
My idea would be to fix it like this, but I am concerned that it might not be the same:
$this->Foo($this->Foo_Id);
PHP doesn't throw the parse/fatal error anymore when I include the class, but like I said, am I going to achieve the same thing as the php4 construct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不了解课程组织的背景,就很难告诉您正确的解决方案。
根据这一点,您可以执行以下操作:
调用“save”方法的代码将获取 Foo 的实例。 从外面看,这可能看起来像:
之前:
之后:
甚至:
也许这对您有帮助。
Without knowing the context in which the classes are organized its hard to tell you the right solution.
Depending on that, you could just do a:
The code which is calling the "save" method will get an instance of Foo. From the outside this could look like:
befor:
after:
or even:
maybe this helps you.
不,不会的。 这只会重新运行构造函数中的任何实际代码(顺便说一句,您可能应该将其重命名为
__construct()
无论如何),而不影响实际未在那里设置的任何属性。No, it won't. That will just rerun any actual code in the constructor (which incidentally you should probably be renaming to
__construct()
anyway) without affecting any properties that aren't actually set there.