Re: Re: [PEPr] Proposal for Database::DB_Table

From: Date: Wed, 07 Jan 2004 16:20:12 +0000
Subject: Re: Re: [PEPr] Proposal for Database::DB_Table
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-24859@lists.php.net to get a copy of this message
On Jan 7, 2004, at 9:56 AM, Lukas Smith wrote:
The factory method can of course be defined in the actual class: class foo { Function __construct() { } Function factory() { } }
This is to illustrate what I mean about factory methods and direct extension: <?php class alpha { function &Factory($args, $classname = 'alpha') { // do stuff return new $classname($args); } function __construct($args) { // do stuff } } class bravo extends alpha { function __construct($args) { // do other stuff } } // returns an 'alpha' object, not a 'bravo' $bravo =& bravo::Factory($args); // returns a 'bravo' object $bravo =& bravo::Factory($args, 'bravo'); ?> Under a factory method, the only way to extend a class directly is to specify the extended class name in the factory call; I find this to be not-that-friendly. Why would I specify the class name when I already "know" what class I'm calling? If someone extends the original class, all the Factory() calls need to be updated to use the new class name. In theory, one might be able to get_class($this) to get the current class name in the Factory() method, but since a factory method is static, there is no $this to look at. If you make the factory non-static, then you might as well make it the constructor. Do you see what I mean? Factories are good in many cases, but in this one (direct extension of a class) I think it's not the right solution. As always, I could be wrong; am willing to be educated. -- Paul M. Jones pmjones@ciaweb.net Savant: the simple alternative to Smarty. http://phpsavant.com/

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