OOP in PHP - why not use it.

Posted on 2007-07-29
Last Modified: 2008-02-01
Hi there :-)

Looking maily at PHP 4 here, I know it's not the greatest OOP language in the world, but still people rave about it (and how it's better in PHP 5) yet lots of big scripts don't really use it. Like PHPBB etc, how come they did't write it like that.

Does anyone do it.

Question by:wildzero
    LVL 35

    Assisted Solution


    Because PHP4 did not do it particularly well or efficiently then this is why generally installs for php4 do not use it,

    because the syntax is different for php5 projects that are aimed at supporting most versions of php on most web servers will avoid it to minimize compatibility issues.

    If you are developing for exclusively for php5 then oop is the way to go
    LVL 10

    Author Comment

    Yea for sure, on local testing server has PHP 5 on it (as it is a real hassle to put PHP 4 on the newer Fedora Core) so looks like we are heading that way.

    Also interesting is the site

    LVL 35

    Expert Comment

    Also note that all the normal reasons for going OOP Apply, ie it is more scalable, it is easier to compartmentalize and to upgrade, and it is also easier to separate content generation and programming logic if you go down the OOP route
    LVL 4

    Accepted Solution

    I find one piece of software the uses classes very well in PHP4 in Invision power boards.

    The use of classes in his scripts was incredible.

    I fully understand the comments that Raynard made about it not working very well but the guy who made Invision used classes to their full potential.

    If you can get hold of an Invision script it is worth your time looking through it, i recently made an admin back end at the company i work for loosley based around the concepts he uses in his scripts.

    PHP5 definately has more to work with in the way of classes though, i primarily come from a .net background that uses classes for just about everything so I can see there is a lot missing from PHP4.

    I do though make sure i use classes for everything now, if i can see a real life situation in a program i am making then i will make a class for it because what may start off as a small project may grow to be quite large and then you will have a lot of code on your hands to change to make it more readable and to customise in the future.

    I dont want any credit for this post because i am basically just going over what Raynard said, i just wanted to mention about Invision.
    LVL 17

    Assisted Solution

    Procedural vs OO programming in PHP is a tightly "contested" debate, and it really boils down to a whole bunch of criteria.

    Up intill PHP5, the way PHP handled classes was not considered to be "true" OO, since each instance of a class was actually a "copy" of the class, meaning that it was basically firstly not as efficient wrt memory usage, and secondly could cause unexpected "bugs" that were often very difficult to track ( due to the fact that overwriting a variable in a new instance did not change the class variable it was derived from.

    PHP5 addressed this problem by having new instances of classes point to the memory address of the "original" class, and overwriting a variable in the derived class also changed the memory of the parent class.

    Having said all that; I still frequently use procedural code even when working with PHP5. I find procedural code "reads" easier, especially in smaller application.

    OO obviously has it advantages, specially due to re-usability and especially in larger application where more than one developer works on the same project.

    In the end, there is really no "right or wrong' here. I'll go out o a limb and say that you can do anything in both styles of coding, in certain instances, OO will be an "overkill", while in others, procedural will take much more effort to do the same thing you did in OO.

    That's what's nice about PHP, the choice is yours.
    LVL 10

    Author Comment

    Hi there,

    Thanks for all the good replies!
    I will be closing this off shorly, I think everything has been covered

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to count occurrences of each item in an array.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now