/    Sign up×
Community /Pin to ProfileBookmark

Class Organisation

Hi, I have read from a few places that it is a best practice to keep each class in a separate file, e.g. class hotelRoom would be in hotelRoom.php or hotelRoom.class.php.

However when you start to read about design patterns and using polymorphism etc this gets a bit complicated. e.g.

[code=php]

abstract class hotelRoom{
private $cost;
}
class singleRoom extends hotelRoom{

}
class doubleRoom extends hotelRoom{

}
[/code]

I do not really want to put all of these classes in their own separate file. Does anybody have an idea of the best practice for doing stuff like this?

I have been thinking of putting every class and interface etc that is related to the hotelRoom in a single php file, is this bad practice?

Thanks in advance

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 08.2010 — I prefer to keep them in separate files. One main reason is that it allows for the use of __autoload() to load class definitions as needed rather than having to manually code a require_once() for each class file.

For organization, you can use a naming convention where you use "_" in the class names to indicate a hierarchy, which is mirrored in your class file directory structure (similar to the PEAR directory structure). So your abstract class might be named "HotelRoom" while your concrete classes would be "HotelRoom_Single" and "HotelRoom_Double". Then your files would be set up as:

<i>
</i>classes/HotelRoom.php
classes/HotelRoom/Single.php (class name: HotelRoom_Single)
classes/HotelRoom/Double.php (class name: HotelRoom_Double)

Then your __autoload() function would be something like:

[code=php]
function __autoload($class)
{
require $_SERVER['DOCUMENT_ROOT'].'/classes/'.str_replace('_', '/', $class).'.php';
}
[/code]
Copy linkTweet thisAlerts:
@DexterMorganauthorDec 08.2010 — @NogDog lol, I knew you was going to be the one to reply, you are the one who always answers my OOP questions.

Yeah your way does look convenient however doesn't having to include 3 (in this case) files decrease performance?
Copy linkTweet thisAlerts:
@NogDogDec 08.2010 — In some cases it might be slower, but in other cases it might be faster. If you have a file with, say, 5 class definitions in it [i]and[/i] your script is going to use all 5 of them, it might be a little faster than 5 separate files. (With modern disk drives and disk caches, this may be a very small difference, though.) But what if a given script uses only 2 of the 5 classes? Now you are loading, parsing, and processing 3 unused classes, which may well take more time than the whatever you saved (if anything) by loading only one file instead of 2.

I guess it's just one of those performance things that I don't worry about, instead concentrating on things like database/query optimization and overall script logic which -- outside of the HTTP request lags -- probably have a much bigger performance impact in most web apps. If you're in a situation where you need to worry about a few milliseconds (or microseconds), then you probably should be using C# or such to create compiled executables. ? Unless/until I encounter a situation where I really have to worry about such tiny performance tweaks, I prefer to design my apps to save [i]me[/i] time rather than that multi-gigabyte CPU. ?
Copy linkTweet thisAlerts:
@DexterMorganauthorDec 17.2010 — Yeah alright, that does make sense, I will try it out on my next personal project.

Thanks a lot for the help!

BTW do you know C# and PHP?

C# is very appealing to me as you can compile your work and then people cant do whatever they want to it. However not a big fan of Visual Studio.
Copy linkTweet thisAlerts:
@NogDogDec 17.2010 — I've done a very little bit in C/C+/C++ (and nothing at all recently), but have never used C#, and none of the others in a web application situation. I guess the only web server-side languages I've used are PHP, Perl, and Cold Fusion, and by far I like PHP the best of those.
Copy linkTweet thisAlerts:
@DexterMorganauthorDec 17.2010 — I've done a very little bit in C/C+/C++ (and nothing at all recently), but have never used C#, and none of the others in a web application situation. I guess the only web server-side languages I've used are PHP, Perl, and Cold Fusion, and by far I like PHP the best of those.[/QUOTE]

Ahh alright no worries, I have only ever worked with PHP and ASP.NET (VB style, in college)

I have heard good things about C# though,
×

Success!

Help @DexterMorgan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...