Welcome Guest, Not a member yet? Register   Sign In
Passing objects around within CodeIgniter
#1

[eluser]TheFuzzy0ne[/eluser]
Hi everyone. I would like some of my controllers, models and libraries to work with objects. Trouble is, I'm not quite sure where to start. However, I do have some specific questions. If a few people could answer them I would really appreciate it.

Where to you define your object? In a library, another file or above the controller?

How do you load your object? Using CIs built-in loader or using the include statement?

Many thanks!
#2

[eluser]kyleect[/eluser]
Most of your classes will be libraries. You can use
Code:
$this->load->library('your_class_name')
to init the object in your controller or model.
#3

[eluser]TheFuzzy0ne[/eluser]
Hi. Thanks for your reply.

The objects I had in mind are used more like simple containers, rather than a library. The object may be instanciated as many times as needed.

Example:
Code:
class Resource {
    var $id = 0;
    var $title = '';
    var $description = '';
}

Sorry for not making myself clearer.
#4

[eluser]Muser[/eluser]
[quote author="TheFuzzy0ne" date="1233950617"]Hi. Thanks for your reply.

The objects I had in mind are used more like simple containers, rather than a library. The object may be instanciated as many times as needed.

Example:
Code:
class Resource {
    var $id = 0;
    var $title = '';
    var $description = '';
}

Sorry for not making myself clearer.[/quote]

It should be a Model
#5

[eluser]TheFuzzy0ne[/eluser]
Good point! I agree.

I'm still not sure how I should instantiate them, however. If I want to instantiate multiple instances of the model, should I load the model using the "include" function, or via CodeIgniter's built-in loader?

Thanks again.
#6

[eluser]kyleect[/eluser]
You can still use pure PHP if that solution works for you. CI isn't designed to replace PHP syntax. You could include and manually init the class into an object.
#7

[eluser]TheFuzzy0ne[/eluser]
Great. I was just growing concerned that bypassing CI might have been considered to be an "ugly hack", to get around a problem that could be solved more elegantly.

Thanks a lot, both of you, for your advice. I really appreciate it.




Theme © iAndrew 2016 - Forum software by © MyBB