Re: Trying to use Component polymorphism, How do I load a component on the fly?
Aaaah. I still have a problem. I can't seem to use components in my sub-classed component. For example, if I have the following: class RedComponent extends ColorComponent { var $components = array('Session', 'Flickr'); ... } I get the folloiwng error when I reference the component from RedComponent, either as $this->Flickr, or $this->controller->Flickr. Notice (8): Undefined property: RedComponent::$Flickr [APP \controllers\components\red.php, line 30] If I define the component in the ColorComponent class, then I can reference it using $this->controller->Flickr, but NOT $this->Flickr. what am I doing wrong??? On Apr 26, 7:06 pm, mixersoft <[EMAIL PROTECTED]> wrote: > I think I have it worked out. > > The following example allows me to load sub-classed Components on the > fly and use them as if they were a "parent class" instance. Note that > components are loaded in the controller through this static command: > > ColorComponent::create($this, 'Red'); > > and are referenced in the controller as: $this->Color > > There might be a more elegant way to do this, but it's good enough for > now. I hope this is of help to others. > > m. > --- > > // file: /app/controllers/components/color.php > > class ColorComponent extends Object > { > var $components = array('Session'); > var $controller; > var $name='Color'; > > function startup(& $controller) { > $this->controller = $controller; > $controller->Color = $this; > // debug("Color startup, controller=".print_r($this->controller- > > >name, true)); > > } > > static function create(& $controller, $color) { > switch ($color) { > case 'Red': > App::import('Component','Red'); > $controller->Color = new RedComponent(); > break; > case 'Blue': > App::import('Component','Blue'); > $controller->Color = new BlueComponent(); > break; > } > $controller->Color->startup($controller); > } > > function getColor() { > return $this->controller->Session->read('Color'); > } > function setColor() { > debug("controller=".print_r($this->controller->name, true)); > $this->controller->Session->write('Color',$this->name); > } > > } > > ?> > > // file: /app/controllers/components/red.php > > class RedComponent extends ColorComponent > { > var $components = array('Session'); > var $controller; > var $name='Red'; > var $init; > > function startup(& $controller) { > parent::startup($controller); > $this->init = $this->name; > }} > > ?> > > > // file: /app/controllers/components/blue.php > > class BlueComponent extends ColorComponent > { > var $components = array('Session'); > var $controller; > var $name='Blue'; > > function startup(&$controller) { > parent::startup($controller); > $this->init = $this->name; > }} > > ?> > > // file: /app/controllers/colors_controllers.php > > class ColorsController extends AppController { > var $name = 'Colors'; > var $components = array('Color'); > var $uses = null; > var $othAuthRestrictions = null; > > function testRed() { > ColorComponent::create($this, 'Red'); > $this->Color->setColor(); > debug($this->Color->getColor()); > exit; > } > > function testBlue() { > ColorComponent::create($this, 'Blue'); > $this->Color->setColor(); > debug($this->Color->getColor()); > exit; > } > > } > > ?> --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Trying to use Component polymorphism, How do I load a component on the fly?
I think I have it worked out. The following example allows me to load sub-classed Components on the fly and use them as if they were a "parent class" instance. Note that components are loaded in the controller through this static command: ColorComponent::create($this, 'Red'); and are referenced in the controller as: $this->Color There might be a more elegant way to do this, but it's good enough for now. I hope this is of help to others. m. --- controller = $controller; $controller->Color = $this; // debug("Color startup, controller=".print_r($this->controller- >name, true)); } static function create(& $controller, $color) { switch ($color) { case 'Red': App::import('Component','Red'); $controller->Color = new RedComponent(); break; case 'Blue': App::import('Component','Blue'); $controller->Color = new BlueComponent(); break; } $controller->Color->startup($controller); } function getColor() { return $this->controller->Session->read('Color'); } function setColor() { debug("controller=".print_r($this->controller->name, true)); $this->controller->Session->write('Color',$this->name); } } ?> init = $this->name; } } ?> init = $this->name; } } ?> Color->setColor(); debug($this->Color->getColor()); exit; } function testBlue() { ColorComponent::create($this, 'Blue'); $this->Color->setColor(); debug($this->Color->getColor()); exit; } } ?> --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Trying to use Component polymorphism, How do I load a component on the fly?
I'm trying to follow your suggestion, but with adjustment so that the component instance is created in either a startup or beforeFilter method, instead of calling new in every action that needs this component. I think I have it done properly, but now I get an error when the Component tries to access it's own $components, in this case Session. Here is what I have: class ParentComponent extends Object { var $components = array('Session'); static function create(& $controller, $type) { switch ($type) { case 'TypeA': if (!($controller->Parent instanceof TypeAComponent)) { App::import('Component', 'TypeA'); $controller->Parent = new TypeAComponent(); } break; case 'TypeB': if (!($controller->Parent instanceof TypeBComponent)) { App::import('Component', 'TypeB'); $controller->Parent = new TypeBComponent(); } break; } $controller->Parent ->startup( $controller); } function startup(& $controller) { if (!empty($controller->type)) { debug("ParentComponent - auto startup for ".$controller->type); ParentComponent ::create($controller, $controller->type); } else { // component reference debug("ParentComponent - basic startup"); $controller->Parent = $this; } $this->controller = & $controller; } } class TypeAComponent extends InterfaceComponent { var $components = array('Session'); function startup() { debug("TypeA startup"); } } class TypeBComponent extends InterfaceComponent { var $components = array('Session'); function startup() { debug("TypeB startup"); } } class MyController extends AppController { var $components('Parent'); var $type; function example() { $this->type = 'TypeA'; ParentComponent::create($this, $this->type); $result = $this->Session->read('Key'); } } and the output I get is: ParentComponent - auto startup for TypeA TypeAComponent constructor TypeAComponent - startup TypeAComponent - startup<--- I don't know why startup is called twice, but it shouldn't hurt Notice (8): Undefined property: TypeAComponent::$Session <--- here is the problem, I cannot access the Session Component, even though it is declared Fatal error: Call to a member function read() on a non-object On Apr 23, 12:25 am, Joel Perras <[EMAIL PROTECTED]> wrote: > App::import('Component', 'MyComponent') willloadthe component. > > In the Component::startup() you should have: > > public function startup(&$controller){ > $this->MyComponent = $controller->MyComponent; > > } > > so that you can then create a component instance when you need it: > > $this->MyComponentInstance = new MyComponent(); > > -J. > > On Apr 22, 9:57 am, Anthony <[EMAIL PROTECTED]> wrote: > > > I'm trying to solve the same exact problem, so far unsuccessfully... > > Any suggestions out there? > > > On Apr 20, 5:44 am, mixersoft <[EMAIL PROTECTED]> wrote: > > > > I have a "parent" component that I want to use to implement an > > > "interface" (java style) and multple "child"componentsthat extend > > > the "interface". I want my controllers to instantiate a specific > > > "child" component, but in the code I want to reference all methods > > > through the "parent" class or "inteface" > > > > class ParentComponent extends Object {} > > > > class TypeAComponent extends InterfaceComponent { > > > function someMethod() { return "a";} > > > > } > > > > class TypeBComponent extends InterfaceComponent { > > > function someMethod() { return 'b';} > > > > } > > > > class MyController extends AppController { > > > var $components('TypeAComponent'); > > > > function example() { > > > $result = $this->Parent->someMethod(); > > > } > > > > } > > > > The example above works fine. But I cannot figure out how toload > > > 'TypeAComponent' or 'TypeBComponent' dynamically. How can I set > > > MyConfroller->componentsBEFORE the controller loads thecomponents? > > > Or how do Iloadadditionalcomponentsafterwards? > > > > I tried this in ParentComponent, but while the subclassedcomponents > > > constructors were called properly, thecomponentswere not "loaded" > > > the same way (i.e. the child component's var $component array were not > > > loaded, i.e. Session.) > > > > class ParentComponent extends Object { > > > static funct
Re: Trying to use Component polymorphism, How do I load a component on the fly?
App::import('Component', 'MyComponent') will load the component. In the Component::startup() you should have: public function startup(&$controller){ $this->MyComponent = $controller->MyComponent; } so that you can then create a component instance when you need it: $this->MyComponentInstance = new MyComponent(); -J. On Apr 22, 9:57 am, Anthony <[EMAIL PROTECTED]> wrote: > I'm trying to solve the same exact problem, so far unsuccessfully... > Any suggestions out there? > > On Apr 20, 5:44 am, mixersoft <[EMAIL PROTECTED]> wrote: > > > I have a "parent" component that I want to use to implement an > > "interface" (java style) and multple "child" components that extend > > the "interface". I want my controllers to instantiate a specific > > "child" component, but in the code I want to reference all methods > > through the "parent" class or "inteface" > > > class ParentComponent extends Object {} > > > class TypeAComponent extends InterfaceComponent { > > function someMethod() { return "a";} > > > } > > > class TypeBComponent extends InterfaceComponent { > > function someMethod() { return 'b';} > > > } > > > class MyController extends AppController { > > var $components('TypeAComponent'); > > > function example() { > > $result = $this->Parent->someMethod(); > > } > > > } > > > The example above works fine. But I cannot figure out how to load > > 'TypeAComponent' or 'TypeBComponent' dynamically. How can I set > > MyConfroller->components BEFORE the controller loads the components? > > Or how do I load additional components afterwards? > > > I tried this in ParentComponent, but while the subclassed components > > constructors were called properly, the components were not "loaded" > > the same way (i.e. the child component's var $component array were not > > loaded, i.e. Session.) > > > class ParentComponent extends Object { > > static function create($type) { > > switch ($type) { > > case 'TypeA': > > > > include_once(dirname(__FILE__).DS.'typeA.php'); > > return new TypeAComponent(); > > break; > > case 'TypeB': > > > > include_once(dirname(__FILE__).DS.'typeB.php'); > > return new TypeBComponent(); > > break; > > } > > } > > > } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Trying to use Component polymorphism, How do I load a component on the fly?
I'm trying to solve the same exact problem, so far unsuccessfully... Any suggestions out there? On Apr 20, 5:44 am, mixersoft <[EMAIL PROTECTED]> wrote: > I have a "parent" component that I want to use to implement an > "interface" (java style) and multple "child" components that extend > the "interface". I want my controllers to instantiate a specific > "child" component, but in the code I want to reference all methods > through the "parent" class or "inteface" > > class ParentComponent extends Object {} > > class TypeAComponent extends InterfaceComponent { > function someMethod() { return "a";} > > } > > class TypeBComponent extends InterfaceComponent { > function someMethod() { return 'b';} > > } > > class MyController extends AppController { > var $components('TypeAComponent'); > > function example() { > $result = $this->Parent->someMethod(); > } > > } > > The example above works fine. But I cannot figure out how to load > 'TypeAComponent' or 'TypeBComponent' dynamically. How can I set > MyConfroller->components BEFORE the controller loads the components? > Or how do I load additional components afterwards? > > I tried this in ParentComponent, but while the subclassed components > constructors were called properly, the components were not "loaded" > the same way (i.e. the child component's var $component array were not > loaded, i.e. Session.) > > class ParentComponent extends Object { > static function create($type) { > switch ($type) { > case 'TypeA': > > include_once(dirname(__FILE__).DS.'typeA.php'); > return new TypeAComponent(); > break; > case 'TypeB': > > include_once(dirname(__FILE__).DS.'typeB.php'); > return new TypeBComponent(); > break; > } > } > > } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Trying to use Component polymorphism, How do I load a component on the fly?
I have a "parent" component that I want to use to implement an "interface" (java style) and multple "child" components that extend the "interface". I want my controllers to instantiate a specific "child" component, but in the code I want to reference all methods through the "parent" class or "inteface" class ParentComponent extends Object {} class TypeAComponent extends InterfaceComponent { function someMethod() { return "a";} } class TypeBComponent extends InterfaceComponent { function someMethod() { return 'b';} } class MyController extends AppController { var $components('TypeAComponent'); function example() { $result = $this->Parent->someMethod(); } } The example above works fine. But I cannot figure out how to load 'TypeAComponent' or 'TypeBComponent' dynamically. How can I set MyConfroller->components BEFORE the controller loads the components? Or how do I load additional components afterwards? I tried this in ParentComponent, but while the subclassed components constructors were called properly, the components were not "loaded" the same way (i.e. the child component's var $component array were not loaded, i.e. Session.) class ParentComponent extends Object { static function create($type) { switch ($type) { case 'TypeA': include_once(dirname(__FILE__).DS.'typeA.php'); return new TypeAComponent(); break; case 'TypeB': include_once(dirname(__FILE__).DS.'typeB.php'); return new TypeBComponent(); break; } } } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Cake PHP" group. To post to this group, send email to cake-php@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---