CodeIgniter Forums
Where to put classes of business logic? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Where to put classes of business logic? (/showthread.php?tid=11837)

Pages: 1 2


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]meglio[/eluser]
If I have developed PHP class (for business logic) with my own constructor (with obligatory parameters), I can't put it to libraries because I then can't load it using

Code:
$this->load->library(...);

This code will raise warning:
Quote:Missing argument 1 for <MyClassName>::__construct(), called in...

Also I can't put this class to helpers - because CI documentation declares helper as collection of functions and not classes.

So where must I put my business logic objects?

P.S. ... answer or just direct me to another topic discussin similar question. Unfortunately I have not found any.

Thanks,
Anton


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]xwero[/eluser]
You can add parameters with the load->library method if you put them in the second parameter. The restriction is it has to be an array.


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]Colin Williams[/eluser]
It looks like your constructor is expecting parameters, and $this->load->library() doesn't automatically pass parameters when instantiating your class (you can pass exactly one parameter to your constructor in the second argument of load->library('classname', 'one_arg'))


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]meglio[/eluser]
Colin Williams, I understand the problem. It is why I'm asking for the solution ideas here.

xwero, I know how to pass parameters, but I do not need it. I do not want to create instance of this object as field of CI instance.


I do not need my class to be created by CI engine. All I need is this class to be available in my code, so I can create instances, call static methods etc. I DO NOT NEED one main instance of this class created in CI (eg for $this).

So the main question is where to put my classes related to business logic and developed to not be as part of CI engine.


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]xwero[/eluser]
If you have one parameter you can add a check to see if the first parameter is not an array and make all other parameters optional. This is a small change.

If you don't want to change your class you can include it as you would in plain php and instantiate it yourself. Another solution is to bend the rules an put the class in a helper this way you have the advantage it doesn't gets loaded more than once and you instantiate it yourself.


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]meglio[/eluser]
[quote author="xwero" date="1222350510"]
If you don't want to change your class you can include it as you would in plain php and instantiate it yourself.[/quote]

I understand that this is possible, but I do not know where can I include class? That is what I'm asking

[quote author="xwero" date="1222350510"]
Another solution is to bend the rules an put the class in a helper this way you have the advantage it doesn't gets loaded more than once and you instantiate it yourself.[/quote]

So I can declare classes in *_helper files? Hm... that is where I was confused. In the CI documentation I just found that helpers are simply colletions of functions and not of classes. So returning back to my reasons, that is why I'm asking this question here.


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]xwero[/eluser]
To include just use the php function include, CI's loader methods are all based on that function.

I think it's not wise to declare your class in a helper. Then you can only instantiate it once like the CI classes. Just put you class code in a helper and then load the helper. The initialization happens in the controller.


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]meglio[/eluser]
So... can I put class declaration to helper and then just use the class after loading of the helper?


Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]xwero[/eluser]
Code:
// class_helper.php
class Class
{
   // go nuts
}
// controller
function function()
{
   $this->load->helper('class');
   $class = new Class();
}



Where to put classes of business logic? - El Forum - 09-25-2008

[eluser]meglio[/eluser]
This is the answer to my question!
I was thinking that this is not correct usage of helpers entities.

Thanks!