Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

trying to get things right with OOP

12:15 pm on Aug 6, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:May 16, 2009
posts: 86
votes: 0

I finished studying the theory for OOP for PHP5, I'm still trying to understand the real utility if this instead of procedural code.
The reason why I wanted to switch is because doing website for years in PHP it always happened that once I had to change something without watching the code for weeks I was lost, I couldn't understand my code, mostly because there was a lot and all disorganized.

Now I'm in a situation where I'm making this site where an user can insert data divided in 5 steps. How exactly can I divide this if not in the procedural style with if or switch statements for the steps?

Maybe I'm just missing the point of OOP, I got the re-usability thing and I'm using it but for me its just calling same functions to feed data to the html page without making sql queries everytime in every page.
Plus I'm used to write (for a section for example) all the code all in one page to avoid making infinite files but maybe I should start doing the opposite? I'm really confused

I just want an understandable code to maintain. Maybe I should start using a PHP framework? I saw some Igniter features and seems pretty cool, but I'm sure I will waste too much time learning the system right now that doing this -kinda urgent- site myself.

Any suggestions? Maybe some tutorials that can clear my mind in using OOP in real life situations?
8:05 pm on Aug 8, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 30, 2007
votes: 0

There isn't something in the php5 that will help you write better oop. It's the principles of oop that can be implemented even in earlier versions of php and perhaps will help to utilize the new functionality of php5.

The main thing to remember is to keep the code simple and easy to use. So for a form with several steps

// Get the current step from the form
$form_request = isset($_GET['form_request'])?$_GET['form_request']:'';

// steps using switch
switch($form_request) {
case 'step1':
// Step-1 code
case 'step2':
// Step-2 code
// Print an error if the form was posted

// using a class with methods
$cForm = new form_process();
if( method_exists($cForm, $form_request) )
} else {
// error

or you can take advantage of the magic methods and handle errors in the class having in your main script just a couple of lines:
$form_request = isset($_GET['form_request'])?$_GET['form_request']:'';
$cForm = new form_process();

and a sample class

class form_process {
function step1() {}
function step2() {}
function step3() {}

//default handling overloading
function __call($method, $args) {
echo '<br />Invalid Form Method Specified<br/>';
8:50 pm on Aug 8, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
votes: 0

Hi there Sandro87,

I'll confess, I'm still having difficulty with some of the procedures & ways of interacting within classes at the moment.

But one thing I can state now is this; Don't write the class just for PHP5 only, if you do your hard work isn't backwards compatible with previous versions of PHP(4). I have just found this out, and from now on, I write the same class for both versions, perhaps this is over kill, but for now this is proving a very useful learning tool for me - so that for my next foray into classes I can write 1 file with elements of both 4 & 5 compatibility in there so that I can invoke functions & procedures depending on what version of php the script is running on.

Hope you understand where I am coming from, I just say this because I am having your exact same issue at the moment - so to break the deadlock, I'm doing something else ;)

And @enigma1, thanks for that insight there, quite useful to see that - a couple of nice pointers there :)

I shall have to read up on __call [php.net ] as I haven't yet used this; and not really sure how to either ;-p

All good fun though..