AUTOLOAD (was: PHP sucks...)

Tim Sweetman ti at lemonia.org
Sat Jan 13 10:47:02 GMT 2007


chromatic wrote:
> On Friday 12 January 2007 11:50, Uri Guttman wrote:
> 
> 
>>how could you make them work if the AUTOLOAD decides on the fly whether
>>to support a method or not? AUTOLOAD has to do the method work itself
>>and it does not have an api just for testing if a method exists. you
>>could write an AUTOLOAD that even checks its args as to whether to
>>implement a method or die and can() will never be able to work around
>>that. AUTOLOAD would need another api which is called by can() so it can
>>return true/false there. maybe call it AUTOEXISTS :).
> 
> 
> Fun fact: can() is a method.  You can override methods in Perl!

Hey, you should know better than that, you wrote the UNIVERSAL::can 
module, to get around the problem of people saying

   if (UNIVERSAL::can($object..) { ... }

and getting the wrong answer (or, at least, not the answer they wanted).

(though as a longtime mod_perl developer, I get distinctly nervous about 
modules which redefine things in the UNIVERSAL package).

> I presume anyone smart enough to write code in AUTOLOAD() which knows which 
> methods to support is smart enough to figure out how to share that same logic 
> with a custom can() method [...]

Simple use of AUTOLOAD is trivial.

Dealing with AUTOLOAD's effects on inheritance, the can method, and 
other subtleties is enough to make experts go "hmm", consider, talk a 
lot, write utility modules, and debate tickets 
(http://rt.cpan.org/Public/Dist/Display.html?Name=UNIVERSAL-can)

Damian's "Best Practices" say: don't use AUTOLOAD.

I never use it, and recommend people against its use, because once 
inheritance and cans are in the mix, the situation becomes extremely 
unclear.

ti



More information about the london.pm mailing list