Tuesday, February 22, 2011

Re: Loading components inside components and keeping the initialize() stack order

Because the SecondaryComponent::specialMethod() method is proper to
the SecondaryComponent, I don't want to put code in MainComponent that
should be be in SecondaryComponent. I would like to keep all code
relating to SecondaryComponent inside SecondaryComponent.

On 16 fév, 19:24, Miles J <mileswjohn...@gmail.com> wrote:
> Why don't you just place the specialMethod() code into initialize()?
> Since thats basically what you are trying to achieve.
>
> On Feb 16, 2:51 am, Pixelastic <timcc.pe...@gmail.com> wrote:
>
> > Hello,
>
> > I'm writing a MainComponent that will need a SecondaryComponent in
> > order to correctly work.
> > I want to call some of SecondaryComponent::specialMethod() in
> > MainComponent::initialize(), but this method can only correctly work
> > if SecondaryComponent::initialize() is itself called first.
>
> > Diving into code-land, here is what I mean.
>
> > class FoosController extends AppController {
> >         var $components = array('MainComponent);
>
> > }
>
> > class MainComponent extends Object {
> >         var $components = array('SecondaryComponent');
>
> >         function initialize(&$controller, $options) {
> >                 $this->SecondaryComponent->specialMethod();
> >         }
>
> > }
>
> > class SecondaryComponent extends Object {
>
> >         function initialize(&$controller, $options) {
> >                 // Some really important stuff must go here
> >         }
>
> >         function specialMethod() {
> >                 // This method can't work properly if the initialize() method hasn't
> > be fired first
> >         }
>
> > }
>
> > I expected the stack order to call SecondaryComponent::initialize()
> > then MainComponent::initialize() but it appears to call
> > MainComponent::initialize() and then SecondaryComponent::initialize(),
> > causing SecondaryComponent::specialMethod() to fail.
>
> > I "fixed" it by manually calling $this->SecondaryComponent->initialize() in MainComponent::initialize(), but I still wonder if
>
> > there would be a more cakish way of doing that.
> > I'm not sure if this behavior is a bug, a design decision, an
> > ommission or simply a wrong approach of myself.
>
> > Has anyone some insight of this ?

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: