homepage Welcome to WebmasterWorld Guest from 54.243.17.133
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
trying to get things right with OOP
Sandro87




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

Hello,
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?

 

enigma1




msg:4183922
 8:05 pm on Aug 8, 2010 (gmt 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
process_step1();
break;
case 'step2':
// Step-2 code
process_step2();
break;
default:
// Print an error if the form was posted
break;
}

// using a class with methods
$cForm = new form_process();
if( method_exists($cForm, $form_request) )
$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();
$cForm->$form_request();

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/>';
exit();
}
}

Matthew1980




msg:4183932
 8:50 pm on Aug 8, 2010 (gmt 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..

Cheers,
MRb

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved