Welcome Guest, Not a member yet? Register   Sign In
Using Code Igniter without the $ this - > load() pattern?
#1

[eluser]TheseAreTheFables[/eluser]
I really like Code Igniter, mainly for its ease of use and clear documentation. I tried Kohana as well but I don't quite trust it since the documentation is so incredibly poor. It makes me feel the developers lack motivation or support, and that the whole project won't last.

So I want to use Code Igniter, however the one thing I don't like about it is the way classes are loaded. The $this->load() pattern seems a bit hackish and cumbersome. So I was wondering, is there any known way to load class in a "normal" way. i.e. $test = new Class() ?
Perhaps some library or plugin that would enable this on CI? If not, any suggestion on the best practice to use "new" operators in CI instead of $this->load?
#2

[eluser]toopay[/eluser]
If, you're choose CI as your framework, i'm affraid at some points, you will be dictate by its convention pattern and coding standard. Its apply to all framework, i believed.
#3

[eluser]pickupman[/eluser]
CI is still php so both syntax options will work. Keep in mind the purpose of this method is to be able extend classes, and using a singluar syntax will work for both classes (system files or extended MY_). Say you want to add an additional feature or modify what a system class does.
Code:
class MY_Session extends CI_Session{
  public function __construct(){
    parent::__construct();
  }

  public function my_new_method(){
    //do something real cool here.
  }
}

Now that I have extended the session class by using load(). I can use the same syntax to call methods from both clasess.
Code:
//CI Syntax
$this->load->library('session');

$this->session->userdata('some_key');
$this->session->my_new_method('some cool again');

//Conventional PHP
require_once(BASEPATH . 'system/core/Session.php');
require_once(BASEPATH . 'application/core/MY_Session.php');

$session = new CI_Session();
$session->userdata('some_key');

$my_session = new MY_Session();
$my_session->my_new_method('some cool again);

It's just less work and easier to use the syntax as documented. Also, by using $this-> you are using global scope, and can be accessed throughout your application as compared to a private scope of $session->.
#4

[eluser]toopay[/eluser]
Also, if you just started, you may find General Style and Syntax at user guide useful for your further CI usage.
#5

[eluser]TheseAreTheFables[/eluser]
[quote author="pickupman" date="1305393683"]CI is still php so both syntax options will work. Keep in mind the purpose of this method is to be able extend classes, and using a singluar syntax will work for both classes (system files or extended MY_). Say you want to add an additional feature or modify what a system class does.
Code:
class MY_Session extends CI_Session{
  public function __construct(){
    parent::__construct();
  }

  public function my_new_method(){
    //do something real cool here.
  }
}

Now that I have extended the session class by using load(). I can use the same syntax to call methods from both clasess.
Code:
//CI Syntax
$this->load->library('session');

$this->session->userdata('some_key');
$this->session->my_new_method('some cool again');

//Conventional PHP
require_once(BASEPATH . 'system/core/Session.php');
require_once(BASEPATH . 'application/core/MY_Session.php');

$session = new CI_Session();
$session->userdata('some_key');

$my_session = new MY_Session();
$my_session->my_new_method('some cool again);

It's just less work and easier to use the syntax as documented. Also, by using $this-> you are using global scope, and can be accessed throughout your application as compared to a private scope of $session->.[/quote]

Thanks for the clarification. Actually, is it possible to make the loader assign the class instance to a local variable, instead of attaching it to the controller? Often I use a new class locally and then I don't need it anymore. Or if I want to make it a member of the class, I will do so explicitly.

i.e. Something like below (I know the syntax is incorrect but you get the idea):

Code:
$session = $this->load->library('session');
$session->userdata('some_key');

Can something equivalent to that be done in CI?
#6

[eluser]WanWizard[/eluser]
No, as said by others, this is simply not how CI works.

CI's architecture defines all classes to be singletons, assigned to the "CI superobject", which is the instance of the controller loaded.

If you have a problem with the way this works, you might have picked the wrong framework. The excellent manual isn't going to do you much good if you decide to do something completely different.




Theme © iAndrew 2016 - Forum software by © MyBB