Welcome Guest, Not a member yet? Register   Sign In
Adding traditional object support to CI
#1

[eluser]Unknown[/eluser]
Upon trying to develop a website using CI for the first time, and coming from an OOP background, I was very disappointed at both CI not having native object(by object I mean instantiable classes) support, and not being able to find a good(not janky) solution for it. So I spent a couple hours to create what I feel is a pretty good fix. And all it involves is adding a few files and a hook.

Instructions:
1. Put the MY_Loader.php file in your application/libraries folder. Make sure the prefix (the MY_ part)on the file is the same as is defined in your config file.

2. Put the Object.php file into your application/libraries folder.

3. Put the load_object.php file into your application/hooks folder.

4. Add the code from the hooks.php file to your application/config/hooks.php file.

5. In your application/config/config.php file set $config['enable_hooks'] = TRUE;

6. Add an objects folder to the application folder. Put all objects in there.

And there you have it. You can now load or autoload objects.

Example of Usage:

Inside autoload.php:
Code:
<?
$autoload['object'] = array("Object_one");
?>

MyObject.php:
Code:
<?
class MyObject extends Object {
  function __construct() {
    parent::__construct();

    $this->load->helper("url");
    echo anchor("foobar.php", "test");
  }
}
?>

Inside Controller:
Code:
<?
$this->load->object("MyObject");
$MyObject_instance1 = new MyObject();
$MyObject_instance2 = new MyObject();
$Object1_or_something = new Object_one();
?>

For a more technical breakdown of what I did, it's not much. I copy and repurposed the view function from the Loader class so that it would load files from the objects folder instead. I then took the Module class and pretty much directly copied it to create the Object class. All the hook does is call load_class("Object", FALSE).

On a side note I would suggest using namespaces for your objects to avoid class name collisions.

Hopefully nobody else has done something like this already because well... actually I wouldn't care all that much. This is for your enjoyment. Please leave comments/suggestions/code fixes/yada yada although I may or may not care about this thread.
#2

[eluser]n0xie[/eluser]
Isn't this the same as 'libraries' ?
#3

[eluser]Unknown[/eluser]
Not really. The libraries serve a slightly different function than objects. With a library, you load it and then CI automatically creates an instance of it and stores it inside the controller. This is useful for certain kinds of classes. But if you need a class that is going to be instantiated more than once, then this will let you load the file in a non-janky, non-instantiating way, and then you can use the class as you see fit.
#4

[eluser]freeman[/eluser]
Hi, I'm sorry if i miss something; but isn't this and that are the same?

Code:
$agent =& load_class('User_agent', 'libraries');
echo $agent->browser();




Theme © iAndrew 2016 - Forum software by © MyBB