魔术方法是 PHP 中的最佳实践吗?

发布于 2024-08-30 16:47:00 字数 25 浏览 8 评论 0原文

魔术方法是 PHP 中的最佳实践吗?

Are Magic Methods Best practice in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

高跟鞋的旋律 2024-09-06 16:47:00

缺点

  1. 文本搜索找不到功能

  2. 系统更难理解,尤其是对于新手来说

  3. 重构工具可能会更频繁地失败

通常,魔术方法在幕后做事,程序员可能没有意识到它正在发生,这使得调试变得更加困难。

当搜索函数(或其他符号)无法找到所有匹配项时,删除旧代码将成为一场噩梦,并且这种恐惧可能会导致死代码堆积在代码库中。如果删除死代码,可能会导致未知位置的损坏。

cons

  1. Text searches don't find the functions

  2. System is harder to understand, especially for newcomers

  3. Refactoring tools might fail more often

Generally, the magic methods do things behind the scenes and the programmer might not realize it's happening which makes debugging harder.

When searching for the functions (or other symbols) can't find all the matches it becomes a nightmare to remove old code and this fear can cause dead code to pile up in the codebase. If the dead code is removed, it can cause breakage in unknown places.

迷乱花海 2024-09-06 16:47:00

我不认为魔法方法是最好或最差的做法:取决于你想要实现的目标,你可以使用或不使用它们......
我的意思是,您不必尽可能调整代码来使用它们,但如果必须的话,根本没有问题。

如果你有一个具有 3 个且只有 3 个属性的对象,则不需要使用魔法 setter/getter,但在某些高级情况下,它们是完成非常复杂的事情(ORM 系统等...)的好方法。

也许其中一些是否已被弃用,我不知道,但大多数都没有。

I don't think magic methods are best or worst practice: depending on what you want to achieve you can use them or not...
What I mean is that you don't have to tweak your code as possible to use them, but if you have to there is no problem at all.

If you have an object with 3 and only 3 attributes you don't need to use magic setters/getters, but in some advanced cases they are a great way to do very complex things (ORM systems etc...)

Maybe some of them are deprecated, I don't know, but most of them are not.

只怪假的太真实 2024-09-06 16:47:00

至少,其中一些神奇的功能是谷歌推荐的:

避免编写幼稚的 setter 和 getter

用 PHP 编写类时,您可以节省时间并加快速度
通过直接使用对象属性来编写脚本,而不是
编写幼稚的 setter 和 getter。在下面的例子中,狗
类使用 setName() 和 getName() 方法来访问名称
属性。

类狗{
  公共$名称='';

  公共函数 setName($name) {
    $this->name = $name;
  }

  公共函数 getName() {
    返回 $this->name;
  }
}

请注意,setName() 和 getName() 只不过是存储和
分别返回 name 属性。

$rover = newdog();
$rover->setName('rover');
echo $rover->getName();

直接设置调用name属性即可运行
速度提升高达 100%减少开发时间。

$rover = newdog();
$rover->name = 'rover';
echo $rover->名称;

原始链接:http://code.google.com/speed/articles/optimizing -php.html

存档版本:https://web.archive.org/web/20120208060457/http://code.google.com/speed/articles/optimizing-php.html

无论如何,这些方法可能不会表现出色,但它们根本没有被弃用。

At least, some of these magic functions are recommended by Google:

Avoid writing naive setters and getters

When writing classes in PHP, you can save time and speed up your
scripts by working with object properties directly, rather than
writing naive setters and getters. In the following example, the dog
class uses the setName() and getName() methods for accessing the name
property.

class dog {
  public $name = '';

  public function setName($name) {
    $this->name = $name;
  }

  public function getName() {
    return $this->name;
  }
}

Notice that setName() and getName() do nothing more than store and
return the name property, respectively.

$rover = new dog();
$rover->setName('rover');
echo $rover->getName();

Setting and calling the name property directly can run
up to 100% faster, as well as cutting down on development time.

$rover = new dog();
$rover->name = 'rover';
echo $rover->name;

Original link: http://code.google.com/speed/articles/optimizing-php.html

Archived version: https://web.archive.org/web/20120208060457/http://code.google.com/speed/articles/optimizing-php.html

Anyway, these methods might not be performant, but they ain't deprecated at all.

緦唸λ蓇 2024-09-06 16:47:00

我不这么认为。我的 IDE 无法向我显示魔术 setter 和 getter 的“提示”。尽管代码有时更难调试。

我宁愿不使用它们,更好地通过我的IDE生成所需的方法(如许多设置器和获取器)。

I don't think so. My IDE is not able to show me "hints" for magic setter and getters. Altough the code is harder to debug sometimes.

I prefer not using them, better generate needed methods (like many setters and getters) by my ide.

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