View Full Version : object oriented php strategies

08-06-2008, 09:48 PM

Basically, I and another person are trying to develop a web site that deals with networking users around particular theme (yoopad.com, networking Christians around missions - what you would see there is our first attempt: now we are re-thinking and trying again.) Currently, much of what we do is procedural programming. This is my first time using php, and I've slowly been learning.

About 2 weeks ago, I decided to look at object oriented php. It seems very useful, and I've tried it out by writing a handful of 'modules' - php classes - to preform different tasks on our website.

Basically, I am hoping for pointers on how to do this right, or at least well.
If anyone can recommend any good books, that would be great as well.

On of these modules, for instance, is called onto any page and allows a user to create a new event, for instance (via a form). I know this was not recommended in the oophp tutorial I saw to but "views" together with other code, but I stored the php form and database handling and the html for the form in the same php class. The php class is initialized, and then different functions are called, for instance, there is a css_function to allow the module to write or include any css it needs, then a js_function and an html_function. There is also a php_function that is put before the header that handles the form results (the form posts to the same page it is on), and then returns those results (the database id of the new event) to the page that included and instantiated the php module. In this way, I could include this event form in many different areas and use the results in different ways.

Here is a rough outline of what a class would look like:

class myclass{

var $size;
//... other vars here

function myclass($size, $event){
//...init code here

function php_function(){
//...Handle form results, if a POST is set: return results
//handle form post here

function css_function(){/*...*/}

function js_function(){/*...*/}

function html_function(){
<form action="myPage.php" method="post" enctype="multipart/form-data" name="testForm">
//... more code here...

And of how it would be used:


$app = new myclass("small", 28);

if($result = $app ->php_function()){
//handle results here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<?php $app ->css_function();?>

<?php $app ->js_function();?>


<h3 align="center">New Event</h3>

<?php $app->html_function();?>


It works fairly well - it is impressive seeing things just work way more than with procedural programming. And now different classes have distinct functions, and there is a clear culprit when something is going wrong.

My code looks a little strange and maybe unconventional... that's why I am asking about it.

Does anyone have any suggestions? Or, as I said above, and good books on the proper way to do this? By the way, our server is running php4 :o - how important is it we get a server with php 5?

Thanks if anyone has any advice.


08-07-2008, 06:10 AM
You're missing an ?> before you're doctype, but I figure this is an example for our benefit.
This looks fine. I'd recommend a PHP5 server, PHP 4 OO is sketchy at best, while PHP5 has a fully supported object core.
The problem with modular objects in PHP4 is you're loss of core control. Since objects in php4 do not allow scope control, any added module can obtain full control of any other module at any given time. This is a nightmare if you allow user added modules (something like facebook for example).
PHP5 also allows you to create a pattern called an object factory. This lets you create 'like' objects dynamically by extending classes and implementing interfaces in a way that makes all like objects the same functionally.
Just because a tutorial tells you about something you shouldn't do, certainly does not mean you cannot do it. Don't take them too much to heart, unless their written by zend developers - I'd go with their recommendations then. I see no issue with creating something like a news posting object on the fly with submitted data. This lets you validate it using the class itself, which in itself has its own benefits.

The only thing you really need to remember is that php is a static language. When you create an object, it does not stay in memory until you release it, its released at the end of any script termination. Unlike a desktop app, you can keep plugging data into the same object until you specify when it should be removed. Stay out of that trap and you'll be alright.

08-07-2008, 05:02 PM
Thanks a lot. I will keep giving this a try.
I am kind of from Saskatoon as well - or at least that area. I am not there now, though.

08-07-2008, 05:43 PM
Thats kewl, I'm from just north in Martensville now, but I was originally from Saskatoon. I should move though, I hate our winters >.<

Yeah, see what you can do. As I mentioned, PHP5 OO is superior in every way compared to PHP4. You can still do a lot of it in PHP4 code, but the scope loss always frightens me. I don't believe PHP4 supports any type of extensions either, and that will increase the amount of code you require since you can't redefine and retarget you're specific needs. A quick (not really feasible) example would be a simple user:

class User
protected $userid;
protected $username;
class RegisteredUser extends User
private $password;
private $email;

The first one is feasible for someone like a guest, while the second is feasible for anyone who has a password and email on top of a userid and username. Since the RegisteredUser extends user it has access to anything that is public or protected as a part of itself, and can still be compared against a user:

$guest = new User();
$user = new RegisteredUser();
printf("Guest is a user? %d\n", ($guest instanceof User)); // 1
printf("User is a User? %d\n", ($user instanceof User)); // 1

Its all of these little things that really make the PHP5 OO shine. Now that PHP 5+ supports custom object typehinting in parameters, it makes it really easy to make comparisons and pass correct datatypes. My wishlist for PHP oo is class scoping, nested classes and Generics.
PHP has really evolved as an OO language. I'm quite impressed with it actually.