CodeIgniter Forums
Make own objects - 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: Make own objects (/showthread.php?tid=19575)

Pages: 1 2


Make own objects - El Forum - 06-13-2009

[eluser]TheFuzzy0ne[/eluser]
Sorry about that, it was just a bit of sarcasm.

Are you running PHP 5? Also, where are you defining your __autoload() function?


Make own objects - El Forum - 06-13-2009

[eluser]juan1904[/eluser]
Yes I am running PHP 5 and my __autoload() is defined in MY_Controller.php which all my controllers are extending.

If i'm putting require_once "system/application/libraries/objects/game_serie.php"; in the controller which loads the page it works perfectly fine.


Make own objects - El Forum - 06-13-2009

[eluser]TheFuzzy0ne[/eluser]
It's a function, not a method, and therefore needs to be defined in the global scope.


Make own objects - El Forum - 06-13-2009

[eluser]juan1904[/eluser]
This is maybe a stupid question but isn't method and function the same thing, just different names? If not, what is the difference?


Make own objects - El Forum - 06-13-2009

[eluser]TheFuzzy0ne[/eluser]
Semantically they are the same, but in actuality methods belong to objects.


Make own objects - El Forum - 06-13-2009

[eluser]juan1904[/eluser]
Ok, I put the function outside the object in MY_Controller.php, and now I get this error message:
A PHP Error was encountered

Severity: Warning

Message: require_once(system/application/libraries/objects/CI_DB.php) [function.require-once]: failed to open stream: No such file or directory

Filename: libraries/MY_controller.php

Line Number: 4

Fatal error: require_once() [function.require]: Failed opening required 'system/application/libraries/objects/CI_DB.php' (include_path='.;C:\php5\pear') in C:\wamp\www\newnhl\system\application\libraries\MY_controller.php on line 4

I've searched for CI_DB.php but I don't know what that has to do with anything since I'm not trying to include that file.


Make own objects - El Forum - 06-13-2009

[eluser]wiredesignz[/eluser]
CodeIgniter is not PHP5 compatible. It uses PHP4 style "class_exists()" function calls in other libraries which will initiate a call to your __autload function and cause you errors.

Alter your __autoload function to prevent CI from using it.
Code:
/** Library object class autoload **/
    function __autoload($class) {
        
        /* don't autoload CI_ or MY_ prefixed classes */
        if (strstr($class, 'CI_') OR strstr($class, 'MY_')) return;
            
        if(is_file($location = APPPATH.'libraries/objects/'.$class.EXT)) {
            include_once $location;
        }        
    }

You could also place this function into the MY_Controller class and then add the PHP5 spl autoload register function into global context:
Code:
spl_autoload_register('MY_Controller::__autoload');