Got this from Daniel Pope’s blog…
- Grab the nearest book.
- Open it to page 56.
- Find the fifth sentence.
- Post the text of the sentence in your blog along with these instructions.
- Don’t dig for your favorite book, the cool book, or the intellectual one: pick the CLOSEST.
Similarly, a class that wraps an HTTP request (including headers, response codes, and so on) has only one instance per request.
The book that was nearest to me was Advanced PHP Programming by George Schlossnagle (ISBN 0-672-32561-6).
I posted this a few days ago, inspired by David Otton’s post on this topic. A chap called Will commented about it, and after doing a quick Google, I got angry and realised how* many people are misinformed that stdClass is the base PHP class for everything.
As of the latest stable release of PHP, this is not the case. And it bugs me when people say “stdClass is the base class of everything in PHP”. stdClass is actually just a basic object with no methods or properties that you can use however you wish. A common use is to create a quick on-the-fly object, e.g.:
$obj = new stdClass();
$obj->Name = 'foobar';
$obj->Id = 24;
$json = json_encode($obj);
As you can see, $obj in this context is throwaway and is just used and forgotten about. I’ll re-iterate what David Otton says about it, and is quick and simple proof:
class DoesNotExtend {};
class DoesExtend extends stdClass {};
$doesNotExtend = new DoesNotExtend();
$doesExtend = new DoesExtend();
var_dump($doesNotExtend instanceof stdClass);
var_dump($doesExtend instanceof stdClass);
Outputs:
bool(false)
bool(true)
So please, when someone asks “what is stdClass” etc., don’t say “Oh, it’s the base class for all objects”, because it bloody well isn’t.
* Jaxxed seems to be slightly more well-informed, but that post was back in 2005. Since then, PHP 5 has come out, and classes are implemented much better. He’s still wrong though, it’s not the base class.
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!
A guy called David Otton contacted me today with regards to a list he’s got of PHP Developers in Portsmouth. It’s kind of wierd that I made it onto the list of this sorts, but I suppose thats what I am - a PHP developer… in the Portsmouth area… I suppose I just didn’t expect anyone to read this blog. I know not many people do, but I’m not in it for fame and fortune… just to write stuff down I suppose. Anyway, check out the list, there’s some interesting sites there.
-
Hampshire Web Developers
As this message states, the new seperator for namespaces in PHP will be a backslash (\). I’ve had a brief skim over the IRC log the message mentions, and the conversation is indeed interesting. Personally, having been converted to a full OOP PHP programmer, I would’ve voted for namespaces only in classes (as classes are all I ever use nowadays…).
Evidently there are too many problems using the Paamayim Nekudotayim due to scope, and static classes and whatnot, but I don’t understand why when real programming languages such as C++ manage it fine. I have to agree with “dmitry”, I think using a backslash is ugly, and double colon is much cleaner.
What does this mean for the PHP community? Well anyone currently using namespaces will have to fix their code, and anyone not using namespaces will have to get used to using the backslash separator. Of course this change is going to ruffle a fair few feathers - people will wonder what on earth possessed them to use the backslash instead of double colon when backslashes are so ugly, and double colon is so… well… standard I suppose.
As this Slashdot post shows:
use My::CPAN::Module qw();
my $instance = My::CPAN::Module->new("junk");
System.Windows.Controls.Listbox box = new System.Windows.Controls.Listbox();
// or
using System.Windows.Controls;
ListBox box = new ListBox();
ABC::bar();
// or
using namespace ABC;
bar();
$object_instance = new My\PEAR\Module("myvar");
// or
using My\Pear;
$object_instance = new Module("myvar");
I doubt people will stop using PHP though. I expect people will either not use namespaces (explicitly anyway - technically they’d just be writing in the global namespace), or get used to it. But then, one must think - what if there is a big backlash against this change and PHP is just a bubble waiting to be burst? I doubt that’d happen, but it’s interesting to wonder…