Welcome Guest, Not a member yet? Register   Sign In
Where to put classes of business logic?
#11

[eluser]xwero[/eluser]
It's not the 'correct' usage of helpers but no harm no foul. The documentation is a guideline for using the framework. You use the tools that are available to the best of your creativity Wink
#12

[eluser]wiredesignz[/eluser]
You can also load the class as a plugin, it will not be instaniated by CI
#13

[eluser]Référencement Google[/eluser]
I personally see nothing wrong including them like this in a controller:
Code:
require_once(APPPATH.'libraries/Someclass.php');

And them instantiate them using conventional PHP.
#14

[eluser]xwero[/eluser]
That was my second suggestion but meglio wants to do it CI wise i guess.
#15

[eluser]Référencement Google[/eluser]
then he can include in from a MY_Controller there would be nothing wrong about that
#16

[eluser]Dready[/eluser]
Hello,

that's really not a correct usage of helpers. I see two more elegant solutions :

Autoload your libraries with autoload functions

Perhaps you already know it, something like :

helpers/autoload_helper.php
Code:
function __autoload($class_name) {
    if ( is_file(APPPATH.'/libraries/'.$class_name.'.php') ) {
          require_once APPPATH.'/libraries/'.$class_name.'.php';
    }
}

And in config/autoload.php
Code:
$autoload['helper'] = array('autoload');

Create a factory class

If you have many business classes, using a factory pattern can help you to have a common way to load all libraries.

libraries/factory.php
Code:
class factory {
  function create($class_name,$your,$parameters) {
    // security checks on $class_name here
    ...

    require_once APPPATH.'/libraries/'.$class_name.'.php';

    return new $class_name($your,$parameters);
  }
}

in controllers :
Code:
$this->load->library('factory');
$myobject =& $this->factory->create('myObject','foo','bar');
#17

[eluser]Référencement Google[/eluser]
I like the idea of a factory class, very smart Dready!




Theme © iAndrew 2016 - Forum software by © MyBB