views:

60

answers:

2

I'm new to PHP and I'm trying to build a site for the first time without using a framework (n.b. I have nothing against frameworks, I just think I should learn to code from scratch before learning a framework on top of it. Sort of like learning Javascript before learning JQuery).

I like OOP in concept, so I started there. I'm thinking of having a catch all script determine which page type is needed, then passing the (sanitized) script input into the class that handles it, where the class is a subclass of a generic Page class which includes common methods to handle things like sending HTTP codes and with an "virtual" display method to actually display the page.

My problem is that it seems like a code smell to have so much happening within the page class itself. Obviously things like talking to databases, sanitizing input, etc. would all be in separate classes, but the individual page classes seem like they'd be huge to contain everything needed to determine the content of the displayed page.

Additionally, I have a bit of a problem convincing myself that there's any need for non-private methods inside either Page or its children. I think this is a side-effect in my thinking due to PHP being a scripting language. Since it's a script, it's non-interactive and input data is determined when the script starts (i.e. POST and GET vars). It seems that with no outside influences on a Page except for other objects where the Page is taking in data, rather than providing it, there is no need for a public method except for the constructor, obviously, and the generate method, which I feel could simply be called from within the constructor.

I feel like this is a somewhat defensible position, except I ran into this question which seems to indicate that private methods need not be unit tested. In my example, this would lead to a Page class that would only be unit tested via a single "display" method, which definitely seems wrong to me.

So tell me: Is my design ridiculously smelly? How can I refactor this into something sane?

+1  A: 

If you don't want to use an existing framework, that's commendable; however, there's nothing wrong with using the principles that the employ. I suggest implementing your own MVC. This will help you not only understand the language, but will make the later adoption of a framework that much easier.

Many sources on how to do this exists, here's one. And another.

Justin Johnson
From your first link: "1. Model handles all our database logic. 2. Controller represents all our business logic 3. View represents our presentation logic i.e our HTML/XML/JSON code." This makes sense to me, and I already figured out these separations by intuition, but the problem seems confined to #2. The page classes seem to be "controllers", but the problem remains that each of these page controllers would be monolithic and without a discernable need to be anything but private. Maybe I'm just missing something fundamental.
Junior Programmer
@JuniorProgrammer If that's what is written in these links, they are wrong. The Model is your business logic, including but not limited to code required to access the persistence layer. The controller is only responsible for handling Requests coming from a UI and delegating these to the appropriate Model code. Controllers and Views should only contain logic regarding the presentation layer.
Gordon
@Gordon That depends on your approach. Some people like fat controllers and skinny models, some like skinny controllers and fat models (I prefer the later)
Justin Johnson
Gordon
+4  A: 

Private methods are somewhat more difficult to unit-test and monolithic classes are usually an indicator for a lack of Separation of Concerns in the architecture. Classes should have distinct responsibility. Grouping too much responsibility into one class often creates a God Object. Try to follow established design patterns to tackle complexity in the heart of software.

Gordon