Welcome Guest, Not a member yet? Register   Sign In
how to assign the instantiated object to a variable?
#1

[eluser]ajsie[/eluser]
in php i can do like this

$object = new Model();
echo gettype($object); // prints Object

i have tried this in CI:

$object = $this->load->model('my_model');
echo gettype($object); // prints NULL

how can i return the created object? cause i want to pass the newly created object as a parameter to another object.
#2

[eluser]stuffradio[/eluser]
Why do you want to do that and what are you trying to accomplish with that?
#3

[eluser]ajsie[/eluser]
[quote author="stuffradio" date="1271069357"]Why do you want to do that and what are you trying to accomplish with that?[/quote]

as i said, i want to pass the object into another object's constructor.
#4

[eluser]adamp1[/eluser]
But why? If its a model you want to pass just do this.
Code:
$this->load->model('my_model');

$object = new MyOtherOBject();

// MyOtherObjectDeclaration
class MyOtherObject
{
    public function __construct()
    {
        $CI = &get;_instance();

        // You now have access to the model object we created before creating this one.
    }
}
#5

[eluser]mddd[/eluser]
Models in CI are not really meant to be used like this. If you load a model with
Code:
$this->load->model('some_model');
then it is available als $this->some_model.
You can use that variable to call a function, or create another object if you want:
Code:
$something = new Object($this->some_model);
But that is not really neccessary because you could also just say
Code:
// main code:
$something = new Object();

// code in Object class:
$CI =& get_instance();
$some_model = $CI->some_model;
#6

[eluser]Phil Sturgeon[/eluser]
The correct way to do this is:

Code:
$this->load->model('my_model');
$object = $this->my_model;
echo gettype($object);    // prints Object




Theme © iAndrew 2016 - Forum software by © MyBB