Welcome Guest, Not a member yet? Register   Sign In
Dynamic/variable model | variable child objects
#1

[eluser]loosetops[/eluser]
In PHP you can access object properties and methods by using variable like this

Code:
$this->$property; // valid
$this->{$property}; // valid

and

Code:
$this->$method(); //valid
$this->{$method}(); // valid

What I need is to access a child object (specifically a model)
Code:
$this->name_model->method(); // valid

but with the name of the model held in a variable.

Code:
$object_name = 'name_model';

$this->$object_name->method(); // not valid
$this->{$object_name}->method(); // not valid

Thanks.

Edit: All the above is valid. I just needed some sleep and a little note below made me see the bug.
#2

[eluser]WanWizard[/eluser]
What is the error you're getting?

This works without problems here:
Code:
// load the rbac library
$test = 'load';
$this->$test->library('Rbac');

Even this works:
Code:
// load the rbac library
$test1 = 'load';
$test2 = 'library';
$this->$test1->$test2('Rbac');
#3

[eluser]WanWizard[/eluser]
** double post - sorry **
#4

[eluser]loosetops[/eluser]
Thanks. Just made me realise I was doing

$this->$var-> instead of $this->ci->$var-> as the call was inside a library.




Theme © iAndrew 2016 - Forum software by © MyBB