PHP 中的私有方法何时应作用于类变量,何时应将此类方法用作函数?
我想知道什么时候私有/受保护的方法应该在类的变量上工作(例如 $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要您确定每个实例只需要维护一个结果变量,则始终使用第一个版本(适用于类属性)。这是使用类的目的之一:拥有共享变量。
您实际上可以将类属性视为隐式指定的方法的参数。 (如果您使用 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.)仅使用您给出的示例,两种方法之间没有任何区别。您可以推断以发现差异,但它们实际上并不存在。
我的猜测是,您正在尝试实现一个类而不先设计它。由于情况并不完整,因此没有明显的理由选择一种方法而不是另一种。
当您开始明确自己想要做的事情时,您可能会发现方法一将过滤例程绑定到类,而方法二则鼓励例程与类分离。
方法一
方法二
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
Method two
filterResults
is no longer a logical name but rather something likeapplyXyzFilter
, which is hard to justify keeping in the class