Welcome Guest, Not a member yet? Register   Sign In
Object in CodeIgniter
#1

[eluser]Josh K[/eluser]
I'm sure this has been asked before, but how do I create new Objects and where do they go?

For example, I have a Question class that I'm using to hold several variables. I want to make an array of Question objects.

Standard PHP5 after loading (or defining an _autoload function):
Code:
$q[] = new Question("topic", "question", "author", "date");
$q[] = new Question("topic2", "question2", "author2", "date2");
// Etc...

How can I do this in CI, and where would I be placing the Question.php object?
#2

[eluser]adamp1[/eluser]
I create a new folder inside the application folder called Entities (thats what I call objects which are just data containers).

Then if the entity is going to be used site wide I include the following line in MY_Controller.php.
Code:
require_once APPPATH . 'Entities/Question.php';

This way the class declaration is known to all controllers and can be used throughout the code.
#3

[eluser]mddd[/eluser]
adamp1's answer seems to be a good solution to me, if you want to use objects directly.

Another way you could go is to put that logic in a Model. And then make a method in that model, like 'add_question'.
So you could write:
Code:
$this->question_model->add_question("topic", "question", "author", "date");
$this->question_model->add_question("topic2", "question2", "author2", "date2");
$q = $this->question_model->get_all_questions();




Theme © iAndrew 2016 - Forum software by © MyBB