Welcome Guest, Not a member yet? Register   Sign In
How to load a class?
#1

[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

class Csv_parser
{
    
    var $mFldSeperator;
    var $mFldEnclosure;
    var $mFldEscapor;
    var $mRowSize;
    var $mHandle;
    var $mRowCount;
    var $mSkippedRowCount;
    var $mSkipEmptyRows;
    var $mTrimFields;
    
    function Csv_parser($file, $seperator = ',', $enclose = '"', $escape = ''){
        
        $this->mFldSeperator = $seperator;
        $this->mFldEnclosure = $enclose;
        $this->mFldEscapor = $escape;
        
        $this->mSkipEmptyRows = TRUE;
        $this->mTrimFields =  TRUE;
        
        $this->mRowCount = 0;
        $this->mSkippedRowCount = 0;
        
        $this->mRowSize = 4096;
        
        $this->mHandle = @fopen($file, "r") or trigger_error('Unable to open csv file', E_USER_ERROR);
    }
    
    function test(){
        return "This works";
    }
}
?>

In the controller where I would like to load the class I have:
Code:
<?php
function index()
    {
        $params = array('test.csv', ';', '"' , '\\');
        $this->load->library('Csv_parser',$params);
$this->Csv_parser->test();
    }
    ?>

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
#2

[eluser]johnwbaxter[/eluser]
I think you need to access it using lower case.

So you would want $this->csv_parser->test();
#3

[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?

Smile

Thanks,
Taff
#4

[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(){
        return "This works";
    }

in it, then try and load your lib and try and run the test function.
#5

[eluser]Taff[/eluser]
Hey audio,

Code:
class Csvparser{
        function Csvparser(){
        }
        
        function test(){
            return "something...please";
        }
    }

and my call from my controller looks like:

Code:
$this->load->library('Csvparser');
$this->Csvparser->test();

and I am getting an error saying:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Katalog::$Csvparser

Filename: controllers/katalog.php

Line Number: 101

Fatal error: Call to a member function test() on a non-object in C:\xampp\htdocs\relaunch\ci\system\application\controllers\katalog.php on line 101

Any ideas?

Thanks for your help this far!

Taff
#6

[eluser]johnwbaxter[/eluser]
You've removed the underscore from Csv_parser, have you renamed the file too?
#7

[eluser]johnwbaxter[/eluser]
@Taff

I've done this and it works just fine.

Katalog.php - Controller file (capital K)

Code:
<?php

class Katalog extends Controller
{

function index()
    {
        $params = array('test.csv', ';', '"' , '\\');
        $this->load->library('Csv_parser',$params);
        $this->csv_parser->test();
    }

}
?>

Then i've got Csv_parser.php - library file in /applications/libraries/

Code:
<?php

class Csv_parser
{
    
    function test()
    {
    return "This works";
    }

}
?>

Can you try the same?
#8

[eluser]elvix[/eluser]
not 100% sure, but maybe try adding this at the beginning of your Katalog controller:

Code:
function Katalog()
    {
        parent::Controller();    
    }
#9

[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.
#10

[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!




Theme © iAndrew 2016 - Forum software by © MyBB