How to load a class? |
[eluser]Taff[/eluser]
Hi, I am having some severe difficulty loading a simple class into CodeIgniter. I have created a file called Csv_parser.php which is in my applications/libraries folder. Code: <?php In the controller where I would like to load the class I have: Code: <?php but no matter I try I get Message: Undefined property: Katalog_Manager::$Csv_parser Can anyone tell me where I am going wrong? This OOP is all a bit new to me I'm afraid. Thanks, Taff
[eluser]johnwbaxter[/eluser]
I think you need to access it using lower case. So you would want $this->csv_parser->test();
[eluser]Taff[/eluser]
Thanks for the reply. Unfortunately that doesn't seem to make any difference. I've been reading the forums and I have a feeling I'm not initialising it correctly...the problem is how do I? Thanks, Taff
[eluser]johnwbaxter[/eluser]
This is what initialises it "$this->load->library('Csv_parser',$params);" I really can't see any problem with what you've done to be quite honest! What if you remove everything from the library and just leave: Code: function test(){ in it, then try and load your lib and try and run the test function.
[eluser]Taff[/eluser]
Hey audio, Code: class Csvparser{ and my call from my controller looks like: Code: $this->load->library('Csvparser'); and I am getting an error saying: Quote:A PHP Error was encountered Any ideas? Thanks for your help this far! Taff
[eluser]johnwbaxter[/eluser]
You've removed the underscore from Csv_parser, have you renamed the file too?
[eluser]johnwbaxter[/eluser]
@Taff I've done this and it works just fine. Katalog.php - Controller file (capital K) Code: <?php Then i've got Csv_parser.php - library file in /applications/libraries/ Code: <?php Can you try the same?
[eluser]elvix[/eluser]
not 100% sure, but maybe try adding this at the beginning of your Katalog controller: Code: function Katalog()
[eluser]johnwbaxter[/eluser]
That's only required if you want to autorun resources when the controller initialises. What i have done works fine. I'm pretty certain it is a capital letter (or lack of) somewhere.
[eluser]Tom Glover[/eluser]
That should be in all your controllers. But on the other hand have you read the bit in the user guide about libraries. A Lib's file name should be the same as the class name and if required, for loading parameters, the first function. Code: $this->load->library(’Csv_parser’,$params); //this loads the lib for use in the controller Code: $this->Csv_Parser->test();// this load and runs the function test from the class Csv_parser http://ellislab.com/codeigniter/user-gui...aries.html http://ellislab.com/codeigniter/user-gui...aries.html Hope this helps you! |
Welcome Guest, Not a member yet? Register Sign In |