Bryce910 Posted April 19, 2012 Share Posted April 19, 2012 I am curious when you guys write php applications and you use OOP do you guys put all your classes in 1 file or do you separate each class into a different file? Which way is perferred by professional programmers? Quote Link to comment https://forums.phpfreaks.com/topic/261282-oop-php/ Share on other sites More sharing options...
Jessica Posted April 20, 2012 Share Posted April 20, 2012 Each class in it's own file. Quote Link to comment https://forums.phpfreaks.com/topic/261282-oop-php/#findComment-1338936 Share on other sites More sharing options...
xyph Posted April 20, 2012 Share Posted April 20, 2012 A file for each class. Every class in a single file means you have to include every class, even if you only want to use a couple of them. Debugging a 10,000+ line file would be UGLY Quote Link to comment https://forums.phpfreaks.com/topic/261282-oop-php/#findComment-1338941 Share on other sites More sharing options...
creata.physics Posted April 20, 2012 Share Posted April 20, 2012 I have at least 5 classes in a file called library.php, this contains a few factory and static classes which are used quite often. I have a file called singletons.php which contains about 3 singletons inside that file. So it really depends on your coding style and how you want to have everything organized and structured. How you have it set up won't make your code any more/less object oriented. Quote Link to comment https://forums.phpfreaks.com/topic/261282-oop-php/#findComment-1338958 Share on other sites More sharing options...
KevinM1 Posted April 20, 2012 Share Posted April 20, 2012 It's often easier to keep it to one class/file due to __autoload, or its SPL counterpart. Unless you like manually referencing include files. Quote Link to comment https://forums.phpfreaks.com/topic/261282-oop-php/#findComment-1338962 Share on other sites More sharing options...
creata.physics Posted April 20, 2012 Share Posted April 20, 2012 That also depends on how your script is set up. I have many library classes in one file that all have functions that can operate independantly. So once I include the file basically all classes are autoloaded. include_once( 'library.php' ); form_lib::function( ); user_lib::function( ); date_lib::function( ); Although the very beginning of this page: http://php.net/manual/en/language.oop5.autoload.php It says, "Many developers writing object-oriented applications create one PHP source file per-class definition". So really it's up to you. You can do things the way many other developers do them. Or you can find a way that perfectly suits you by creating code to handle these types of instances themselves. Quote Link to comment https://forums.phpfreaks.com/topic/261282-oop-php/#findComment-1338964 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.