From:             rayro at gmx dot de
Operating system: Any
PHP version:      Irrelevant
Package:          Class/Object related
Bug Type:         Feature/Change Request
Bug description:Create objects with array of parameters

Description:
------------
Currently it is (not possible) to create an object using an array of
parameters.
You have to type in each parameter for the constructor manually... (see
script #1)

As it is possible to call functions using call_user_func_array, it would
also be nice to create objects like that. The only trick to make this
possible is to make the use of Reflection. (see script #2) 

The paranthesis indicate that the arguments for __construct will be passed.
The idea is to omit the paranthesis and pass the arguments as array. (see
script #3)

But this is only my idea... Maybe the best way is to provide a function for
that, like call_user_func_array() for functions exists... (see script #4)

Additionally another idea is to change the way "new" works, enabling to
pass parameters to new. (see script #5)

But for me, #3 is the best solution, because IF it would be possible in
future releases of php to cast to an object, the new keyword can simply be
omitted while making the use of paranthesis. (see script #6)

thx for listenig :)

Test script:
---------------
#1
<?php
$obj = new MyClass(1,4,8);
?>

#2
<?php
$class = new ReflectionClass('MyClass'));
$obj = $class->newInstanceArgs($array);
?>

#3
<?php
$obj = new MyClass $array;
?>

#4
<?php
$obj = create_instance('MyClass', $array);
?>

#5
<?php
$obj = new(MyClass, $array); # possible solution 1
$obj = new('MyClass', $array); # possible solution 2
?>

#6
<?php
$object = (MyClass) $array;
?>

Expected result:
----------------
A function or language construct to provide making objects of classes
quickly.

Actual result:
--------------
No way instead of making the use of Reflection...

-- 
Edit bug report at https://bugs.php.net/bug.php?id=63693&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=63693&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=63693&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=63693&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=63693&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=63693&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=63693&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=63693&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=63693&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=63693&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=63693&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=63693&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=63693&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=63693&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63693&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=63693&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=63693&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=63693&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63693&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=63693&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=63693&r=mysqlcfg

Reply via email to