Nov 11 2008

As David Otton points out, PHP is different from most other OO languages such as Java, or even Delphi, in the fact that most languages have a base class that all other classes inherit. Indeed, stdClass is not the base class for everything - it is in fact just a backup in case PHP doesn’t really know what class it is - it’s basically just a generic class.

After having used Netbasic’s custom MVC for several months now, which has a base class, I believe there can be merits, especially in a framework situation, to extending a base class such as Object throughout all classes. You can add things like debug code into Object, and it propogates through into all child objects. It’s sometimes a good idea to take this ethic further, such as in .NET, classes are arranged from parent to child according to how specific their function is… i.e.

  • Object - base object
  • Control - a generic control
  • WebControl - a generic web-based control
  • BaseDataBoundControl
  • DataBoundControl
  • ListView - the list view object

I suppose how your classes are structured should be dictated primarily on what your application requires, and secondly on how you like them structured… it’s preference at the end of the day!

3 Responses to “PHP Base Classes”

  1. Will says:

    To be honest, I am not even sure most PHP ‘programmers’ even know what stdClass is. Perhaps you can write an introduction to it?

  2. James Titcumb » Blog Archive » stdClass is NOT the PHP Base Class!!! says:

    [...] posted this a few days ago, inspired by David Otton’s post on this topic. A chap called Will commented [...]

  3. Base classes in OOP programming languages | CodeUtopia says:

    [...] you don’t explicitly define it. They also tend to have somewhat deeper inheritance chains, as James Titcumb points out, in reply to David’s [...]

Leave a Reply