sylvertwst Posted November 1, 2008 Share Posted November 1, 2008 hello all, i was wondering what you would consider best practise to do: when i have multiple classes, i was thinking of making one php file per category, for example i would have one database connection class and a query class in dbconnect.php, and then user class and login class in user.php. is this a good way of doing things? would yousuggest a new document for each class used? one document for all classes in a single class library? thanx in advance beginner OO php Sylvertwyst Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/ Share on other sites More sharing options...
Daniel0 Posted November 1, 2008 Share Posted November 1, 2008 I think it's better with one class per file. Otherwise you might end up with huge files. It's also easier to load e.g. the class User if you know it is in User.php. Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/#findComment-679991 Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2008 Share Posted November 1, 2008 And you can use the autoload function to cause the file to be loaded when you create an instance of the class - http://us.php.net/manual/en/language.oop5.autoload.php Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/#findComment-679997 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 I do one class per file. Together with SPL autoloading feature works nice. Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/#findComment-679998 Share on other sites More sharing options...
sylvertwst Posted November 1, 2008 Author Share Posted November 1, 2008 cool! thanks for the tip on autoload. when i make a new class that implements another class or interface, can i use separate files as well, do i need to use the autoload function at the top of eacth class file then? or should parent/child classes go in the same document still? Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/#findComment-680003 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 I have this in autoload.php function classAutoload($class_name) { require_once("class.".$class_name.".php"); } spl_autoload_register('classAutoload'); and then I just include('autoload.php') on top of each script. Of course all my classes are in files called 'class.classname.php' Classes extending parent classes should have their own files. Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/#findComment-680006 Share on other sites More sharing options...
sylvertwst Posted November 1, 2008 Author Share Posted November 1, 2008 thank you very much. i will adopt your way of naming on a side note, did you just tell php to use classAutoload() instead of __autoload() to autoload the classes? ps: DanielO i like your signature. particulary the how to ask questions faq link Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/#findComment-680009 Share on other sites More sharing options...
Mchl Posted November 1, 2008 Share Posted November 1, 2008 on a side note, did you just tell php to use classAutoload() instead of __autoload() to autoload the classes? Almost. This way you can actually have more than autoload function, and PHP will try to use all of them one after another. Useful if you use some third party code, that use autoloading as well. Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/#findComment-680011 Share on other sites More sharing options...
sylvertwst Posted November 1, 2008 Author Share Posted November 1, 2008 i can see the use of it thank you for the swift answers everyone <3 phpfreaks Quote Link to comment https://forums.phpfreaks.com/topic/130987-classes-in-separate-files/#findComment-680012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.