Subscribe to How-To Geek

Making use of Object Oriented PHP

This is a guest post by Tim, who has written on Jatecblog before. Tim runs Sacratee.

Since the release of object oriented programming in PHP, it has lead many people to write better and more efficient code, but still many programmers are unaware of this. I will show you a couple ways to make use of this not-so-new way of programming. Note, you must have PHP 5 to use the code below.

Modular System

With many new types of content management systems being created, it is no surprise people are making add-ons for them. This can be a pain if the CMS developer has not put in object oriented ways of streamlining the process of module development.

Below is some example code in which the user can easily add an item to a menu bar. First, the menu class the programmer of the application has implemented before hand:

jklsjdfkljfdklsdjfskldjfskldjjfskld.png

myMod.php

cdfb4.png

As you can see, if makes the task much less tedious for the module developer.

Template System

Using PHP's OOP __ destruct method, we can easily have things run at the end of our script without specifically putting code there.

cdfb3.png

The above code would replace {name} with bob in template.html when outputted. Note, if you remove the reference to the class by overwriting $Template, that will call the __destruct method as well.

So there you go! A couple useful ways to use object oriented programming in PHP.

| More
This article was originally written on 01/6/08 Tagged with: Web Design / Programming

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Comments (3)

  1. Fouf

    Wow, this really cleared up a lot about OOP for me. Thanks a lot, waiting to see more.

  2. Richy’s Random Ramblings / PHP: Making use of Object Orientated PHP

    […] a quickie: From Tim of Sacratee there is a post on Jatecblog about Making use of Object Oriented PHP which provides a good simple example of how to use OOP in PHP5 to build a module-based […]

  3. Alfredo

    The article is nice, the code is ok, but I can't see the "why" of this article.

    I agree that many PHP programmers are unaware of OOP in PHP, but I don't believe that this kind of article shows how to do it. The code is good, you show how to use the __construct, __destruct and __toString methods, although there is no explanation about them.
    I guess this article should be named "How to create a menu bar using OOP in PHP" rather than "Making use of Object Oriented PHP".

    Regards,


Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
mDNSResponder.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.