Re: Beginner Tutorials for using CLASSES in PHP4

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2007-10-10 at 17:46 -0400, Nathan Nobbe wrote:
> On 10/10/07, Robert Cummings <robert@xxxxxxxxxxxxx> wrote:
>         On Wed, 2007-10-10 at 13:30 -0500, Jay Blanchard wrote:
>         > [snip]
>         > so what are the benefits of the "with interfaces" solution
>         over
>         > the"without
>         > > interfaces" solution
>         > [/snip] 
>         >
>         > Polymorphism.
>         
>         Bleh, polymorphism isn't unique to interfaces. In fact, PHP
>         has
>         polymorphism all over the place by virtue of it's loose type
>         system. I
>         can pass ANY object to any function or method (that doesn't
>         use PHP5's 
>         restrictive type hinting stuff) and the code will "just use
>         it" as long
>         as the appropriate member functions/variables exist. PHP
>         doesn't need
>         inheritance or interfaces to achieve this. In fact this is far
>         more 
>         flexible than the restrictive nature of interfaces. Lemme
>         illustrate:
>         
>         class Finger
>         {
>             function wiggle()
>             {
>                 echo 'A finger wiggles ominously.';
>             }
>         }
>         
>         class Earthworm 
>         {
>             function wiggle()
>             {
>                 echo 'An earthworm wiggles around.';
>             }
>         }
>         
>         function wiggle( $something )
>         {
>             if( method_exists( $something, 'wiggle' ) )
>             {
>                 $something->wiggle();
>             }
>             else
>             {
>                 echo 'Nothing happens.';
>             }
>         }
>         
>         $finger = new Finger();
>         $jim = new Earthworm();
>         
>         wiggle( $finger );
>         wiggle( $jim ); 
>         
>         Look Ma, no inheritance, no interfaces, and we have
>         polymorphism. in
>         fact, our wiggle function can take absolutely anything and
>         just try to
>         wiggle it. Undoubtedly OOP purists are going to scream at this
>         because 
>         it just feels wrong, *hah*, too bad :)
>         
> 
> another interesting code example, but  the use of method_exists is in
> userspace and 
> would have to be called everywhere you wanted to use the 'wiggleable'
> interface. 
>
> could you imagine using interfaces with multiple methods and multiple
parameters w/ 
> this technique; i think it would get rather messy.
> 
> interface A {
>    function a($someVar);
>    function b($someOtherVar, $anotherOtherVar); 
>    function c($someStupidVar)
> }
> 
> very easily identified by A.
> 
> vs.
> 
> function doSomethingWithA($classInstance) {
>    if(method_exists($classInstance, 'a') &&
>       method_exists($classInstance, 'b') && 
>       method_exists($classInstance, 'c')) {
>         $classInstance->a('a');
>         $classInstance->b('b', 'c');
>         $classInstance->c('c');
>      }
> } 
> 
> and we dont have the ability to count the number of parameters in each
method
> without using reflection or going into some unorthodox technique; more
specifically,
> php doesnt provide a function similar to func_num_args() for class
methods.  at 
> least i didnt see one here.
> so identifying one method, no parameter interfaces would work with this
technique.
> it doesnt do well for moderately complex interfaces which ill define as
any interface 
> with more than one method where those methods have 1 or more parameters.
yes,
> code using this technique would be quite ugly for such interfaces.
> 
> another benefit of going w/ the language constructs is the existence of
the interfaces, or 
> other classes that would be used polymorphically is that they are defined
outside of the
> code that uses them.  thats a little cleaner.
> i hate digging through code where some functionality is in one place and
then mysteriously 
> its in another; and then a light turns on and i realize its the same
functionality in two different
> places w/o a label to let me know w/o thinking they are the same.
> this example is a little better than that, but still not as nice as the
language keywords. 
> 
> there is a new art that php exposes; one that mingles functions and
objects without
> requiring objects.  functions are not first class citizens but they have a
global visibility.
> one interesting argument is that in java static methods are essentially a
way (the only way) 
>  to provide a global method call.  in php that isnt necessary, a function
could just be written.
> but i still like the static class method; because there is the association
of the function w/ 
> some other functions that belong to the class; there is encapsulation. 
> 
> this example is cool because it illustrates some of the neat things that
can be done w/ phps
> mix of objects and functions.

What I was really illustrating is how interfaces are syntactic
sugar only. In my above example what I've really shown is an
implicit interface :) Since OOP is largely meant to model real
world things, ask yourself this... when a doctor sews a pig's
heart into a human, do you think there's an explicit interface
someplace that checks for compatibility, or does it "just work"
if the conditions are right. Food for thought, pork in fact ;)

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux