Re: [PEPr] Comment on Web Services::Services_TinyURL

From: Date: Tue, 11 Sep 2007 14:48:18 +0000
Subject: Re: [PEPr] Comment on Web Services::Services_TinyURL
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-47975@lists.php.net to get a copy of this message
On Sep 11, 2007, at 9:04 AM, Joe Stump wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
1) What about those of us who like to instantiate everything? :-) Would a Services_TinyURL::singleton/getInstance() be appropriate, or am I the only one that would want to instantiate this object?
I can't think of a strong argument to do so. Can you? I meant for it to be exceptionally lightweight (much like TinyURL itself). I can be persuaded though as I don't feel strongly on the subject. Anyone else feel it should be instantiable (is that a word?)?
The biggest benefit is polymorphism within whatever uses that code. Using static methods ties you to that implementation forcing changing all of the references to it within the code base instead of swapping out the place that it was instantiated. The real-world case for this is unit testing. Static methods make mocking an object while unit testing nearly impossible (it can be done with multi-process tests assuming you create a pseudo-object and place it in the include path in-front of the real implementation or that your code uses an autoloader only when an object doesn't exist). While not something you'll specifically have to worry about, anyone who wants to test a class that uses Services_TinyURL within their class will have to jump through a few hoops to get testing working instead of just passing in a MockServices_TinyURL object. Of course, there's flip sides to these arguments. Java is a great resource for reading the hashed and re-hashed discussions on the theological - err, I mean technical merits (and flaws) of static methods. FWIW: to make the code work as a static or instantiated method, you would want to include this: $me = (isset($this) && get_class($this) == __CLASS__) ? $this : self::getInstance(); Then reference the api var as $me->api instead of self::$api (with the appropriate change to its scope). That means that $obj->create() and Services_TinyURL::create() will function the same without having any specific static binding. That also buys you the ability to sub-class the object and provide a custom api address, something that the late-static binding issues in PHP5 prohibits with the self keyword. -Travis

« previous php.pear.dev (#47975) next »