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 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 [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
-~----------~----~----~----~------~----~------~--~---