Coder Profile - Show off your skills, get a coder profile.
 
 
 
When to use OOP in PHP
Web Development
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 Copy / Restore
  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('to@address.com');
  37. $email->set_from('tfrom@address.com');
  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


Posted By VBAssassin
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
Izzmo     Posted 15 Days Ago
 
 
Well, I've constructed most of my sites in the tier pattern. UI->Business
Layer->Data Layer.

Right now, I have some classes to inherit my user class, which stores username,
userid, and ecetera. When I inherit it and then try to get the public variables from
the user class, it cannot find them.

I've tried many different ways to get the variables to pass through, and they
just won't. Maybe it's my programming, but I doubt it :P
 
VBAssassin     Posted 15 Days Ago
 
 
I would consider PHP5's OOP to be very adequate in regards to OOP. It comes
complete with scopes (private, public, protected), magic methods (constructors,
deconstructors, etc), type of classes (abstract, interfaces, etc) and the good old
class inheritance (including multiple inheritance from classes extending other
classes extending other classes, etc).

I've found creating classes in PHP4 sucks, PHP5 however, excellent!

In regards to porting a website, thats down to the programmer. I've ported
this site (coderprofile) to 3 servers very easily and the base of this site is
procedural... infact... most of it is. It's only widgets and components that i
did as objects for a very good reason.

Kind regards,
Scott
 
Izzmo     Posted 15 Days Ago
 
 
This demonstrates the ease of creating and using classes, but what about when someone
has to organize their site/program in such a way where classes are the only logical
answer? One example: website porting.

Also, PHP has major flaws when it comes to classes, and it's very unfortunate.
PHP isn't really fully OOP yet, but I will be waiting till it does.
Page 1 of 1
More Articles By This Author
Speed Up Delivery of your JavaScript Libraries
AJAX - When to use it?
When to use OOP in PHP
Preventing raw PHP leaks on Apache
What makes a good PHP programmer?
Recently Posted "Web Development" Articles
Speed Up Delivery of your JavaScript Libraries
A cool tool ^^
AJAX - When to use it?
Introduction To PHP
[PHP] XML
HTML vs XHTML
[PHP] XML: HTML DOM Basics
Embed flash into your website
When to use OOP in PHP
Module Development guide for Derangedminds
Recently Rated "Web Development" Articles
When to use OOP in PHP
Preventing raw PHP leaks on Apache
AJAX - When to use it?
Speed Up Delivery of your JavaScript Libraries
[PHP] XML
A cool tool ^^
HTML vs XHTML
Introduction To PHP
Embed flash into your website
[PHP] XML: HTML DOM Basics
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (4)
Web Development (12)
search Search Inside
Web Development
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved