PHP公共函数在类中的顺序是否会影响其执行?

发布于 2024-09-14 01:20:21 字数 1258 浏览 8 评论 0原文

我一直在关注这个 Symfony 教程。在某些部分中,它只是告诉我在 class 中添加一个 public 函数,但没有说明我是否应该将其添加在类的开头或末尾。

例如:

/**
 * JobeetCategory
 *
 * This class has been auto-generated by the Doctrine ORM Framework
 *
 * @package    jobeet
 * @subpackage model
 * @author     Your name here
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class JobeetCategory extends BaseJobeetCategory
{
  public function countActiveJobs()
  {
    $q = Doctrine_Query::create()
      ->from('JobeetJob j')
      ->where('j.category_id = ?', $this->getId());

    return Doctrine_Core::getTable('JobeetJob')->countActiveJobs($q);
  }

  public function getSlug()
  {
    return Jobeet::slugify($this->getName());
  }

  public function getActiveJobs($max = 10)
  {
    $q = Doctrine_Query::create()
      ->from('JobeetJob j')
      ->where('j.category_id = ?', $this->getId())
      ->limit($max);

    return Doctrine_Core::getTable('JobeetJob')->getActiveJobs($q);
  }
}

getActiveJObs公共函数是教程中第一个显示的,countActiveJobs是我根据教程添加的最后一个函数。

类内公共函数的顺序重要吗?

I've been following this Symfony tutorial. In some sections it just tells me to add a public function inside a class but it doesn't say if I should add it at the beginning or at the end of the class.

For instance:

/**
 * JobeetCategory
 *
 * This class has been auto-generated by the Doctrine ORM Framework
 *
 * @package    jobeet
 * @subpackage model
 * @author     Your name here
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class JobeetCategory extends BaseJobeetCategory
{
  public function countActiveJobs()
  {
    $q = Doctrine_Query::create()
      ->from('JobeetJob j')
      ->where('j.category_id = ?', $this->getId());

    return Doctrine_Core::getTable('JobeetJob')->countActiveJobs($q);
  }

  public function getSlug()
  {
    return Jobeet::slugify($this->getName());
  }

  public function getActiveJobs($max = 10)
  {
    $q = Doctrine_Query::create()
      ->from('JobeetJob j')
      ->where('j.category_id = ?', $this->getId())
      ->limit($max);

    return Doctrine_Core::getTable('JobeetJob')->getActiveJobs($q);
  }
}

The getActiveJObs public function was the first shown in the tutorial and countActiveJobs is the last function I added according to the tutorial.

Does the order of the public functions inside a class matter?

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

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

发布评论

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

评论(3

总以为 2024-09-21 01:20:21

类内公共函数的顺序重要吗?

不,事实并非如此。班级整体评价;方法的顺序不相关。

因此,虽然它没有任何约束力,但我遇到的最常见的顺序,也是我最喜欢的排序方法是,

class ClassName 
 {

  - Variable definitions

  - Class constants

  - Constructor 

  - Public methods

  - Destructor (if needed)

  - Magic functions (if needed)

  - Private / helper methods

  }

Does the order of the public functions inside a class matter?

Nope, it doesn't. The class is evaluated as a whole; the order of methods is not relevant.

So while it's in no way binding, the most common order I've encountered, and my favourite of ordering methods is, is

class ClassName 
 {

  - Variable definitions

  - Class constants

  - Constructor 

  - Public methods

  - Destructor (if needed)

  - Magic functions (if needed)

  - Private / helper methods

  }
空城旧梦 2024-09-21 01:20:21

这是一个有很多答案的问题,而且很大程度上取决于个人喜好:-) 从技术上讲,这可能并不重要,但它确实很重要。

这是一个关于结构的建议。想象一下,您是第一次上课。也许您正在调用其公共方法来执行某些任务,现在您想了解更多信息。从顶部的公共变量和函数开始似乎很自然。从抽象的角度思考它。较高的抽象级别在类中处于较高的位置。你越往下走,抽象的层次就越低。因此,从顶部的公共函数到底部,您最终会得到一堆私有函数。这将带来更好的可读性。

同时我们希望其他函数调用的函数能够靠近,因此第一次调用函数时,请将其放在调用函数的正下方。

有些练习将实例变量放在使用它的函数的正上方,但这可能会使类更难以阅读。特别是,如果有多个函数使用同一个实例变量,您将不知道要查找。通常,将所有常量和实例变量放在顶部。

那么我们最终应该得到这样的结果:

class MyClass
{
    const MY_CONSTANT_ONE
    const MY_CONSTANT_TWO

    public $myPublicVariableONe
    public $myPublicVariableTwo
    protected $_myProtectedVariableOne
    private $_myPrivateVariableOne
    private $_myPrivateVariableTwo

    public function DoSomeOfficialStuff()
    {
        $this->_myNicePrivateMethodOne();
    }

    private myNicePrivateMethodOne(){
    }

    public function returnSomeOfficialStuff()
    {
        $this->_myNicePrivateMethodTwo();
    }

    myNicePrivateMethodTwo(){
    }
}

This is a question with many answers and much about personal preference :-) It might not matter technically speaking, but it does matter nevertheless.

Here is one suggestion For structure. Imagine that you are visiting a class for the first time. Maybe you are calling its public methods to perform some task and now you want to understand more about it. It seems pretty natural to begin with the public variables and functions at the top. Think about it in terms of abstraction. The higher levels of abstraction are high up in the class. The further down you descend, the lower the level of abstraction is. So from the public functions on the top you end up with a bunch of private functions in the bottom. This will make for much better readability.

At the same time we want the functions called by other functions to be close, so the first time you call a function, put it right beneath the calling function.

Some practice putting instance variables right above the function that uses it, but this can make the class harder to read. Especially, if there are several functions using the same instance variable, you won't know to look. As a rule, put all constants and instance variables at the top.

Then we should end up with something like this:

class MyClass
{
    const MY_CONSTANT_ONE
    const MY_CONSTANT_TWO

    public $myPublicVariableONe
    public $myPublicVariableTwo
    protected $_myProtectedVariableOne
    private $_myPrivateVariableOne
    private $_myPrivateVariableTwo

    public function DoSomeOfficialStuff()
    {
        $this->_myNicePrivateMethodOne();
    }

    private myNicePrivateMethodOne(){
    }

    public function returnSomeOfficialStuff()
    {
        $this->_myNicePrivateMethodTwo();
    }

    myNicePrivateMethodTwo(){
    }
}
在风中等你 2024-09-21 01:20:21

答案是否定的。这些函数将在其他地方被调用,而不是从上到下执行。 countActiveJobs 位于类的顶部还是底部都没有任何区别。

The answers no. The functions will be called for somewhere else and not execute from top to bottom. It will not make any difference whether countActiveJobs is at the top of the class or at the bottom.

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