# new_object_array()

- Thread: https://externals.io/message/9554
- Original archive: https://news-web.php.net/php.internals/9554
- Started by Julian Reich on 2004-04-25T17:02:40+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #9554 — Julian Reich — 2004-04-25T17:02:40+00:00

URL: https://externals.io/message/9554#9554

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

---

## #9555 — Timm Friebe — 2004-04-25T17:20:11+00:00

URL: https://externals.io/message/9554#9555 · In reply to: #9554

> 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

---

## #9556 — Andrey Hristov — 2004-04-25T17:24:30+00:00

URL: https://externals.io/message/9554#9556 · In reply to: #9555

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

---

## #9557 — Marcus Boerger — 2004-04-25T17:33:12+00:00

URL: https://externals.io/message/9554#9557 · In reply to: #9554

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

---

## #9558 — Julian Reich — 2004-04-25T18:11:33+00:00

URL: https://externals.io/message/9554#9558 · In reply to: #9555

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
