Having classes on the same file?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Having classes on the same file?

Post by kaisellgren »

Hi,

I was wondering that does anyone ever put abstract classes or interfaces in the same file with their "real" class equivalents? This way you could save some execution time, but makes the class file a bit larger..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Having classes on the same file?

Post by Christopher »

That seems wrong because the point of an abstract class is to provide a base class for other classes to extend. If you have the abstract and child class in the same file it implies that they should be combined.

The only time I combine classes is for 1) helper classes used by a larger class that would never need to be autoloaded, 2) combining many classes to speed loading.
(#10850)
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Having classes on the same file?

Post by kaisellgren »

Would this be an acceptable solution for production use (leaving the developer version intact - separate files)?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Having classes on the same file?

Post by alex.barylski »

I keep all classes in separate files and run a compile like script to pull classes into a single file (cached actually not compliled). Each request pulls on different classes, so each request loads a different cache.

The only problem I have ran into using this approach is when the cache is out of date and the classes are not loaded completely...I basically need to run my application through a ringer to ensure the classes loaded on each request are properly cached...which also means I completely test my application.

I`m looking into maybe using Selenium to automate the process as I currently use iMacros for Firefox which is a little tedious.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Having classes on the same file?

Post by kaisellgren »

PCSpectra wrote:Each request pulls on different classes,
How do you determine, which cache to load?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Having classes on the same file?

Post by alex.barylski »

Each request is associated with a URI -- so the cache are just MD5'd URIs
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Having classes on the same file?

Post by kaisellgren »

PCSpectra wrote:Each request is associated with a URI -- so the cache are just MD5'd URIs
:)

Are you sure you never miss any required classes?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Having classes on the same file?

Post by Darhazer »

In the framework we use, small related classes are put together in one class. For example all basic actions (create / execCreate, edit / execEdit, view, delete ) as well as their abstract classes they extend, are in 1 files, but those are type of command patterns and they are really small. Same applies to all validator classes, and to all basic widget classes. When we are creating a set of specific validators that all applies to the same view (screen in the browser) we also put them in one file.

Sometimes module classes are also combined in one file.
Post Reply