关于如何重置类的 php 4 到 5 移植问题

发布于 2024-07-30 05:50:48 字数 412 浏览 7 评论 0原文

有人编写了以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻了热茶 2024-08-06 05:50:48

如果不了解课程组织的背景,就很难告诉您正确的解决方案。

根据这一点,您可以执行以下操作:

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();

也许这对您有帮助。

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:

return new Foo($this->Foo_Id);

The code which is calling the "save" method will get an instance of Foo. From the outside this could look like:

befor:

$instance->save($ID);
$instance->methodFromClassFoo(); # Instance would be now of class foo. (which btw. is really bad design.

after:

$foo = $instance->save($ID);
$foo->methodFromClassFoo();

or even:

$instance = $instance->save($ID); // Instance is now instance of foo.
$instance->methodFromClassFoo();

maybe this helps you.

最单纯的乌龟 2024-08-06 05:50:48

不,不会的。 这只会重新运行构造函数中的任何实际代码(顺便说一句,您可能应该将其重命名为 __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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文