开发者

Does the order of PHP public functions in a class affect its execution?

开发者 https://www.devze.com 2023-01-11 11:21 出处:网络
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

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?


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

  }


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(){
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消