new_object_array()

php.internals

Julian Reich

22 years ago
Hi all, Referring to call_user_func_array() I would like to suggest a new function: new_object_array(). I'm working on a php5 application framework and I would like to implement something I call an unified factory. The problem is that I don't know how many parameters to expect. Here's an example how it's meant to work: <?PHP class Kernel { public function __call($functionName, $functionParameters) { $className = substr($functionName, 3); // code omitted here ... // check for real $className ... return new_object_array($className, $functionParameters); } public function newObject() { $functionParameters = func_get_args(); $className = array_shift($functionParameters); // code omitted here ... // check for real $className ... return new_object_array($className, $functionParameters); } } $Kernel = Kernel::getInstance(); $DbManager = $Kernel->getDbManager('param1', 'param2'); // 1st possibility to get new object $XmlParser = $Kernel->newObject('XmlParser', 'param1', 'param2'); // 2nd possibility to get new object ?> I have written an implementation of new_object_array(); <-- see attachment Perhaps some things need to be rewritten ;) I would be happy if you would consider to include this function in the standard distribution of php. Yours sincerly, Julian Reich

Timm Friebe

22 years ago
On Sun, 2004-04-25 at 19:02, Julian Reich wrote:
> Hi all, > > Referring to call_user_func_array() I would like to suggest a new > function: new_object_array(). > I'm working on a php5 application framework and I would like to > implement something I call an unified factory. The problem is that I > don't know how many parameters to expect. > > Here's an example how it's meant to work: > <?PHP > class Kernel > { > public function __call($functionName, $functionParameters) > { > $className = substr($functionName, 3); > // code omitted here ... > // check for real $className ... > return new_object_array($className, $functionParameters); > }
How about: return call_user_func_array( array(new ReflectionClass($className), 'newInstance'), $functionParameters ); Untested but should work. - Timm

Andrey Hristov

22 years ago
Timm Friebe wrote:
> How about: > > return call_user_func_array( > array(new ReflectionClass($className), 'newInstance'), > $functionParameters > ); > > Untested but should work. > > - Timm >
Yep, I have done something similar and it worked. The Reflection API is quite powerful. Andrey

Julian Reich

22 years ago
Hi all, Hi Timm,
> How about: > > return call_user_func_array( > array(new ReflectionClass($className), 'newInstance'), > $functionParameters > );
I also considered this possibility, but it looked like big overhead. Am I wrong? Yours sincerly, Julian Reich

Andi Gutmans

22 years ago
I agree that it's best to keep this in user-land using the existing reflection API. At 08:11 PM 4/25/2004 +0200, Julian Reich wrote:

Marcus Börger

22 years ago
Hello Julian, adding a new function seens the wron way. I'd prefer adding this functionality to the reflection API. Hande a look at Zend/zend_reflection_api.c: ZEND_METHOD(reflection_class, newInstance) marcus Sunday, April 25, 2004, 7:02:40 PM, you wrote:
> Hi all,
> Referring to call_user_func_array() I would like to suggest a new > function: new_object_array(). > I'm working on a php5 application framework and I would like to > implement something I call an unified factory. The problem is that I > don't know how many parameters to expect.
> Here's an example how it's meant to work: > <?PHP > class Kernel > { > public function __call($functionName, $functionParameters) > { > $className = substr($functionName, 3); > // code omitted here ... > // check for real $className ... > return new_object_array($className, $functionParameters); > }
> public function newObject() > { > $functionParameters = func_get_args(); > $className = array_shift($functionParameters); > // code omitted here ... > // check for real $className ... > return new_object_array($className, $functionParameters); > } > }
> $Kernel = Kernel::getInstance(); > $DbManager = $Kernel->getDbManager('param1', 'param2'); // 1st > possibility to get new object > $XmlParser = $Kernel->newObject('XmlParser', 'param1', 'param2'); // 2nd > possibility to get new object
?>>
> I have written an implementation of new_object_array(); <-- see attachment > Perhaps some things need to be rewritten ;)
> I would be happy if you would consider to include this function in the > standard distribution of php.
> Yours sincerly, > Julian Reich
-- Best regards, Marcus mailto:helly@php.net