![]() |
How to instantiate classes? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: How to instantiate classes? (/showthread.php?tid=12222) |
How to instantiate classes? - El Forum - 10-10-2008 [eluser]sl3dg3hamm3r[/eluser] Hey there Sorry for this newby-question, but how can I instantiate my own classes? Given the following class: controllers\admin\ClassTest.php If I try to instantiate it as following in one of my controllers like: Code: $obj = new ClassTest(); PHP won't find the class, as expected. I read about the __autoload-interceptor, something like this: Code: function __autoload($className) { If I insert this into my controller, still nothing works. How would I need to implement this in order to find classes? Oliver P.S.: I use PHP 5+, of course... How to instantiate classes? - El Forum - 10-10-2008 [eluser]Mirage[/eluser] You don't instantiate your own controllers. CI does it for you. Just make sure you have a matching url for your controller. If you want to use libraries, they shouldn't be in the controller directory. CI creates those instance for you as well if you load them via the loader. If you just want to load a library without creating an instance, call : Code: load_class('ClassTest', false); If you absolutely must load odd stuff from odd locations, use the include* or require* php directives and make sure your class files are in the include path. HTH, -m How to instantiate classes? - El Forum - 10-10-2008 [eluser]sl3dg3hamm3r[/eluser] Thank you for your reply. Let me ask about common practise. I'd like to use the controllers only as helpers which will decide what class needs to be loaded, eventually also checking form-data first, according to the Front-Controller These classes contain business-logic. Managing products, defining relations and rules etc. Now, would you expect such classes rather in the controller-folder, or should I stick them into libraries? I didn't find any documentation about load_class - how can I pass parameters to the constructor? Oliver P.S.: Just figured out that CI expect it in libraries - my first question is obsolete P.P.S.: load_class() acts as singleton?? and what do I need to do if I want several instances? How to instantiate classes? - El Forum - 10-10-2008 [eluser]Mirage[/eluser] load_class is a core helper function. CI core uses this function itself to load classes, etc. However it's not advertised and probably not intended to be used in applications. So perhaps that was bad advice. More like a workaround/hack. As to your approach - It sounds to me like you should create one or more model(s). The controller receives the request, filters and validates it and then invokes functions in the Model to process data. When the model completes the processing, the controller then outputs the view. HTH m How to instantiate classes? - El Forum - 10-10-2008 [eluser]sl3dg3hamm3r[/eluser] You are right, the model-section in the docs sounds more like what I need. Thx! |