Salut

Un débat vient de naître sur les listes de Zend Framework : faut-il utiliser __construct() ou bien init() dans un contrôleur d'action ? Pourquoi l'un plutôt que l'autre ?

La réponse apportée par la majorité des participants est favorable à init(). Cependant, cette méthode est déclarée avec une visibilité "public", ce qui n'a pas d'intérêt puisqu'elle est systématiquement appelée depuis le contrôleur lui-même.

Il a donc été suggéré de la rendre protected dans une version ultérieure.

Si la méthode Zend_Controller_Action::init() devient protected, elle deva être renommée en _init().


En parallèle, Lars Strojny est intervenu pour déconseiller l'existence d'une méthode init(), argumentant que cette méthode a le même objectif que __construct()...

Hi Matthew,

Am Samstag, den 12.07.2008, 14:55 -0400 schrieb Matthew Weier O'Phinney:
> -- Hector Virgen <djvirgen@gmail.com> wrote
> (on Saturday, 12 July 2008, 10:41 AM -0700):
> > If Zend_Form::init() becomes protected, will it be renamed to _init()?
>
> Yes -- but such a change will not happen prior to 2.0 at the earliest,
> if at all.


I have to admit that I find the wide-spread usage of init()-methods
quite unfortunate, as they address a bad programming habit. Not calling
the parent constructor is a bad practice, as it will always break code.
I totally agree that complicated setup code do not belong in the
constructor itself, but the init()-methods are not about complicated
object setups but about allowing the user to forget about
parent::__construct(). So I would really love to see most of them go
away in 2.0.

cu, Lars
Qu'en dites-vous ?