|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2014-07-17 19:15 UTC] requinix@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: requinix
[2014-07-17 19:15 UTC] requinix@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 07:00:01 2025 UTC |
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...