Xdega Posted March 15, 2011 Share Posted March 15, 2011 so I am about to start on a small object oriented project, for practice/learning etc. I have 5 files in a folder called "objects" : (obj1.php obj2.php obj3.php obj4.php obj5.php) . I then created a "classloader.php" with the following code: <?php include "objects/obj1.php"; include "objects/obj2.php"; include "objects/obj3.php"; include "objects/obj4.php"; include "objects/obj5.php"; ?> and then finally my main page (for the sake of testing): <!DOCTYPE HTML> <head> <title>...</title> </head> <?php include "classloader.php"; ?> <body> <header></header> <nav></nav> <section> <?php object1(); object2(); object3(); object4(); object5(); ?> </section> <footer></footer> </body> What is the best way to handle the loading of objects in php? I think I am doing it somewhat wrong. Anyone lend me some more info here? thanks much. Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/ Share on other sites More sharing options...
flolam Posted March 15, 2011 Share Posted March 15, 2011 take a look at autoloading: http://php.net/manual/de/language.oop5.autoload.php Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/#findComment-1187693 Share on other sites More sharing options...
Xdega Posted March 15, 2011 Author Share Posted March 15, 2011 so, I took a look at this. Changed my classloader.php to <?php //DEFINE AUTOLOADER function __autoload($className) { if (file_exists($className . '.php')) { require_once $className . '.php'; return true; } return false; } ?> I also changed the file names/dir to be consistent with the above code. Something like below: index.php --/objects/ ----classloader.php -----object1.php -----object2.php -----object3.php -----object4.php -----object5.php for clarification, here is the relevant content in the index.php <?php include "objects/classloader.php"; ?> <?php object1(); object2(); object3(); object4(); object5(); ?> from the manual, it looked pretty straight forward, but doesn't seem to be working. any ideas what I am doing wrong here? Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/#findComment-1187836 Share on other sites More sharing options...
AbraCadaver Posted March 15, 2011 Share Posted March 15, 2011 include "objects/classloader.php"; $obj1 = new object1(); $obj2 = new object2(); $obj3 = new object3(); $obj4 = new object4(); $obj5 = new object5(); Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/#findComment-1187841 Share on other sites More sharing options...
Xdega Posted March 15, 2011 Author Share Posted March 15, 2011 hmm, that still doesn't seem to be working. And I did try that earlier also. but I am getting a slightly differrent error:class 'object1' not found instead of the previous call to undefined object Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/#findComment-1187859 Share on other sites More sharing options...
ChemicalBliss Posted March 15, 2011 Share Posted March 15, 2011 Check your paths. <?php //DEFINE AUTOLOADER function __autoload($className) { if (file_exists('objects/'.$className . '.php')) { require_once 'objects/'.$className . '.php'; return true; } return false; } ?> Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/#findComment-1187861 Share on other sites More sharing options...
Xdega Posted March 15, 2011 Author Share Posted March 15, 2011 see earlier on I actually tried to do this. I set the path via a var $dir="objects/"; I did for the sake of trying though, try and enter the paths in manually, and the same errors are occuring. Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/#findComment-1187866 Share on other sites More sharing options...
ChemicalBliss Posted March 15, 2011 Share Posted March 15, 2011 Hmm.. Well using all of the input from all the users on this topic it works for me: /index.php <?php include "objects/classloader.php"; $obj1 = new object1(); $obj2 = new object2(); $obj3 = new object3(); ?> /objects/classloader.php <?php //DEFINE AUTOLOADER function __autoload($className) { $file = 'objects/' . $className . '.php'; if (file_exists($file)) { require_once $file; return true; } return false; } ?> /objects/object1.php <?php class object1{ public function __construct(){ echo(get_class($this)); } } ?> /objects/object2.php <?php class object2{ public function __construct(){ echo(get_class($this)); } } ?> /objects/object3.php <?php class object3{ public function __construct(){ echo(get_class($this)); } } ?> Does this work for you? Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/#findComment-1187875 Share on other sites More sharing options...
Xdega Posted March 15, 2011 Author Share Posted March 15, 2011 LOL. I guess that would be my problem. object1.php <?php function object1() { echo "<br>object 1 code here"; } ?> major slapforhead! Link to comment https://forums.phpfreaks.com/topic/230677-loading-self-made-objects-in-php/#findComment-1187877 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.