views:

31

answers:

2

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?

+10  A: 

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

  }
Pekka
@Pekka OK, but according to your experience, for the sake of avoinding confusion and having organization is it better to place new public functions at the top or at the bottom?
janoChen
@jano see my update.
Pekka
@jano ah, you mean within the list of public methods. Hmm, I'd say to the bottom, if you're not grouping by functionality. It might make sense, though, to group all "Jobs" methods and all "Slug" methods etc. closely together.
Pekka
Why not in alphabetical order?
Gumbo
@Gumbo doesn't really make sense in this case IMO - `getActiveJobs()` may be accompanied by `DeleteJob()` and `CopyJobs()` one day, and they should most likely be grouped.
Pekka
@Pekka OK I got it thanks.
janoChen
Personally, I prefer magic methods above public methods, destructor right below the constructor... I also toss static variables first, and static functions above the constructor. It's all personal preference. +1
ircmaxell
A: 

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.

Ash Burlaczenko