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.
---

<?php
// 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);
        }

}

?>


<?php
// 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;
        }
}
?>

<?php

// 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;
        }
}
?>


<?php
// 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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to