Welcome Guest, Not a member yet? Register   Sign In
Object instances and CI superobject
#1

[eluser]ipavlic[/eluser]
Hello everyone.

I am new to CodeIgniter and rather new to php. I have been coding in CI for the last month. I have found the community responses to other people's questions very helpful in resolving various problems I have had along the way.

I am hoping to receive some advice from you on the following problem:

To solve a resource management task, I implemented an algorithm in a .php file (i found it quicker to draft and test the code in basic php and after testing "transfer" the code to CodeIgniter).

The Algorithm class instantiates various classes to store objects needed to solve the resource management problem, and then proceeds to calculate the solution.

I found how to instantiate and use the needed objects in threads Best Practice for instantiating classes in Libraries that are non-singletons? and Best practices using multiple object instances?.

I decided to use load_class
Code:
class Algorithm extends Controller
{
    // variables -- will hold class instances after construction
    function Algorithm()
    {
        parent::Controller();
        load_class('myclasses',false);
        // instantiate other needed classes and assign them to variables
    }
    // functions to calculate the solution
}
in the constructor of the 'Algorithm' controller. The file myclasses contains necessary class definitions.

In order for the classes to instantiate, they need to pull some information from the database, so I would write an algorithm_model.php.
After instantiating, the Algorithm class contains other classes which in turn contain some other classes.

My questions are:
- Would it be more in the spirit of CI to just put the Algorithm class in myclasses, along with the other ones?

Also, I don't quite understand the CI superobject:
- If two users make two requests to various controllers, are then two superobjects created (each including the Controller class instantiated with different parameters)?
- Is then the CI superobject a singleton "per call" (marked for destruction after processing the function call requested via url)?
- And is then the CI "running" for only as long as it needs to complete a single function call of some user?
- Whether I put the Algorithm in myclasses and instantiate it or make it a controller class which gets instantiated by requesting the appropriate url, both the Algorithm class and the class instances it contains as variables are contained in the CI superobject, are they not?

I realize these are probably elementary questions :red:, but am confused with explanations like this one (emphasis mine):
Quote:CI works by building one 'super-object': it runs your whole program as one big object
.
I am grateful for any pointers and clarifications.
#2

[eluser]WanWizard[/eluser]
It's probably easier to turn your other classes in libraries or models, and use them the standard way instead of using load_class() and manual instantiation. load_class() is an internal function that has changed in CI2, it now always instantiates the object (the second parameter is no longer present).
if you need multiple objects you have to manually load them using require_once(APPPATH.'classes/myclass.php') (assuming your classes are in a separate classes directory).

As to you other questions: HTTP is stateless, so every request made to the webserver will load your index.php (and everything after it) as a separate process. So yes, a CI superobject exists per call. When CI has finished processing the request, the script ends and the object and all data are gone. This is not CI specific, this is the way web applications, and PHP is particular, work.

Everything instantiated by CI is part of the CI super object, so it is accessable everywhere using $this. If you instantiate your classes manually, you have to assign them to the CI superobject manually if you want that same behaviour.
Code:
// this is a local object
$my_object = new My_object();
// this is a CI object
$CI =& get_instance();
$CI->my_object = new My_object();
#3

[eluser]ipavlic[/eluser]
[quote author="WanWizard" date="1281183277"]It's probably easier to turn your other classes in libraries or models, and use them the standard way instead of using load_class() and manual instantiation. load_class() is an internal function that has changed in CI2, it now always instantiates the object (the second parameter is no longer present).
if you need multiple objects you have to manually load them using require_once(APPPATH.'classes/myclass.php') (assuming your classes are in a separate classes directory).[/quote]

I decided to make a library out of the 'Algorithm' class, and load the required objects using require_once.
The arrays of instances of various classes are used to store information, so I do need multiple instances of them.

The final structure looks like this:
Code:
/*
/classes
..
class1.php
class2.php...

/libraries
..
algorithm.php
*/

class Algorithm
{
    protected $CI;
    private class1_instances = array();
    private class2_instances = array();
    //other variables
    public function Algoritm($input = array())
    {
        $this->CI =& get_instance();
        require_once(APPPATH.'classes/class1.php');
        require_once(APPPATH.'classes/class2.php'); //...

        $this->initialize_class1_instances();
        $this->initialize_class2_instances(); //...
    }
    // private function initialize_class1_instances()...

[quote author="WanWizard" date="1281183277"]As to you other questions: HTTP is stateless, so every request made to the webserver will load your index.php (and everything after it) as a separate process. So yes, a CI superobject exists per call. When CI has finished processing the request, the script ends and the object and all data are gone. This is not CI specific, this is the way web applications, and PHP is particular, work.[/quote]

Yes, that makes much more sense than the quote i put in my first post.

Thank you very much for your feedback.




Theme © iAndrew 2016 - Forum software by © MyBB