List Info

Thread: Re: Comment on Web Services::Services_TinyURL




Re: Comment on Web Services::Services_TinyURL
user name
2007-09-11 09:49:15
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You've won me over. I'll make the change. I went back and
forth on  
this beforehand and figured it was such a lightweight
package static  
functions would be fine ... 0.0.4 will be forthcoming
tonight.

- --Joe

On Sep 11, 2007, at 7:48 AM, Travis Swicegood wrote:

> 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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Darwin)

iD8DBQFG5qrsh0MUGpYY9OQRAuoJAKCi8siV0qI84ZglZmORevln2nZFgQCc
Cg+3
efBYQjZK85E8dDaPQvC9ZZw=
=tkUk
-----END PGP SIGNATURE-----

-- 
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )