PHP 中的私有方法何时应作用于类变量,何时应将此类方法用作函数?

发布于 2024-09-19 03:53:26 字数 819 浏览 4 评论 0原文

我想知道什么时候私有/受保护的方法应该在类的变量上工作(例如 $this->_results),以及何时应该像函数一样使用此类方法(例如 $ this->_method($results))。下面的示例:

处理类属性

<?php

class TestA
{
    protected $_results;

    public function getResults()
    {
        $this->_results = getFromWebservice();
        $this->_filterResults();
    }

    protected function _filterResults()
    {
        $this->_results = doMagic($this->_results);
    }
}

“作为函数”工作

<?php

class TestB
{
    protected $_results;

    public function getResults()
    {
        $results = getFromWebservice();
        $this->_results = $this->_filterResults($results);
    }

    protected function _filterResults($results)
    {
        return doMagic($results);
    }
}

I'm wondering when private/protected methods should work on the class it's variables (like $this->_results) and when such methods should be used as if they were functions (like $this->_method($results)). Examples below:

Work on class properties

<?php

class TestA
{
    protected $_results;

    public function getResults()
    {
        $this->_results = getFromWebservice();
        $this->_filterResults();
    }

    protected function _filterResults()
    {
        $this->_results = doMagic($this->_results);
    }
}

Work "as function"

<?php

class TestB
{
    protected $_results;

    public function getResults()
    {
        $results = getFromWebservice();
        $this->_results = $this->_filterResults($results);
    }

    protected function _filterResults($results)
    {
        return doMagic($results);
    }
}

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

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

发布评论

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

评论(2

梦一生花开无言 2024-09-26 03:53:26

只要您确定每个实例只需要维护一个结果变量,则始终使用第一个版本(适用于类属性)。这是使用类的目的之一:拥有共享变量。

您实际上可以将类属性视为隐式指定的方法的参数。 (如果您使用 C++ 进行编程,您至少知道确实是这样。例如,您可以通过在方法参数后面放置 const 来指定您不会更改类属性.)

As long as you are sure, that you need to maintain only one results variable per instance always use the first version (which works on the class property). That's one of the purposes of using classes: to have shared variables.

You may actually consider the class properties as arguments to your methods, which are implicitly specified. (If you're programming in C++ you know that at least there it really is that way. For example you can specify that you are not going to change a class property by putting a const after the method arguments.)

↙温凉少女 2024-09-26 03:53:26

仅使用您给出的示例,两种方法之间没有任何区别。您可以推断以发现差异,但它们实际上并不存在。

我的猜测是,您正在尝试实现一个类而不先设计它。由于情况并不完整,因此没有明显的理由选择一种方法而不是另一种。

当您开始明确自己想要做的事情时,您可能会发现方法一将过滤例程绑定到类,而方法二则鼓励例程与类分离。

方法一

  • 有副作用
  • 覆盖原始数据
  • 不能在其他数据上重用

方法二

  • 方法一可以很容易地修改以记住它已经完成了过滤,但方法二会很困难,尤其是在尝试保持抽象时
  • 因为它的抽象, filterResults 不再是一个逻辑名称,而是类似 applyXyzFilter 的名称,很难证明保留在类中是合理的

Using exclusively the example you've given there is no difference whatsoever between the two methodologies. You could extrapolate to find differences, but they are not actually present.

My guess is that you are trying to implement a class without designing it first. Because the picture is not complete, there are no obvious reasons to choose one methodology over the other.

When you start to flesh out exactly what you want to do, you will likely discover that method one binds the filtering routine to the class, whereas method two encourages the routine to be separated from the class.

Method one

  • Works on side-effects
  • Overwrites original data
  • Cannot be reused on other data

Method two

  • Whereas method one could be easily modified to remember that it has already done the filtering, method two will struggle especially when trying to keep it abstracted
  • Because of its abstraction, filterResults is no longer a logical name but rather something like applyXyzFilter, which is hard to justify keeping in the class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文