Coder Profile - Show off your skills, get a coder profile.  
 
 
 
source codes Categories articles
Browse All
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (1)
Operating Systems (3)
Other (1)
Programming (45)
Security (9)
Software Development (4)
Web Development (6)
search Search Inside
Web Development
6.00
out of 10

When to use OOP in PHP


This is purely how I use Objects in PHP and I am not saying this way will be best for you however it has proven to be the better way for myself without the disadvantages of OOP� i.e. spending more time on structure of a program than coding, and having your code jump all over the place from object to object.

My Rules (To prevent complication)

1. Each object ONLY refers to itself i.e. it will not create any new objects inside of itself.
2. Each object achieves just ONE task and does not try to do everything.
3. Objects are used to process data in an already known fashion, NOT to make decisions on what code gets executed as that would be handling the object itself and should not be done inside the object.

Functions

First of all lets decide when functions should be used instead of Object methods containing those functions. If you can do everything you need to via ONE function for example str_replace() to just replace a string it should be a function.. if however you have two or more functions working together then it may be better to set it up as a class.

Function Classes

As mentioned above if you have two or more functions working together for example in emailing you may have 3 functions:

send_email()
add_html_template()
add_attachment()

Now you could do the handling of those in the main php script, but that is messy since you have to pass variables all over the place i.e. in the global scope of the script just to put back in to another function. This is now where you would put them 3 functions in a class and tie everything together using the methods of the class and setting a few variables which will then encapsulate everything! Brilliant!

Here is an example of the email class made from those functions above.
Code :: Class (OOP) - Pseudo Code DownloadOpen In New Window
  1.   class email {
  2.  
  3.           private $subject = '';
  4.           private $content = '';
  5.           private $attachments = '';
  6.           private $to = '';
  7.           private $from = '';
  8.  
  9.           public function set_subject($subject) {
  10.                     $this->subject = $subject;
  11.           }
  12.           public function set_to($address) {
  13.                     $this->to = $address;
  14.           }
  15.           public function set_from($address) {
  16.                     $this->from = $address;
  17.           }
  18.           public function set_content($content) {
  19.                     $this->content = $content;
  20.           }
  21.  
  22.           public function send_email() {
  23.                     mail($this->content . $this->attachments);
  24.           }
  25.           public function add_html_template() {
  26.                     $this->content = "<html>Hello<br /><br />" . $content . "<br /><br />Kind regards,<br />Scott</html>";
  27.           }
  28.           public function add_attachment() {
  29.                     $this->attachments = "attachment here";
  30.           }
  31.  
  32. }
  33.  
  34. $email = new email();
  35. $email->set_subject('Hello');
  36. $email->set_to('[email protected]');
  37. $email->set_from('[email protected]');
  38. $email->set_content('Buddy how you doing?');
  39. $email->add_attachment('me.png');
  40. $email->add_html_template();
  41. $email->send_email();
See how nice it is to use and then see how well it is encapsulated and how the methods all work together? That�s where OOP is useful!

General Logical Classes

This is what I like to call when people do EVERYTHING in OOP. They end up putting structural code inside objects and linking objects to objects� objects inheriting code, results complication.

In PHP it is great that you can handle the objects outside of an object. This allows normal easy to follow code execution (linear) while still making great use of Objects.

That is it for now, I may expand on this article depending on the interest of it.

Kind regards,
Scott

What would you rate this article?
Please login to rate and upload coding articles. To start registering a free account with us, click here..
Please login to post comments.
Page 1 of 1
More Articles By This Author
Recently Posted "Web Development" Articles
Recently Rated "Web Development" Articles
 
 
Part of the MyPingle Network
Make A Donation :: Our Affiliates :: Affiliation Request :: Contact Me
Development Blog :: Terms & Conditions :: Privacy Policy :: Documents
Version 1.39.05
Copyright � 2007 - 2008, Scott Thompson, All Rights Reserved