Please see the code bellow:
01. class Test {
02. public function __construct($param1, $param2, $param3) {
03. echo $param1.$param2.$param3;
04. }
05. }
06.
07. $params = array('p1','p2','p3');
08.
09. $ob = new Test;
10.
11. if(method_exists($ob,'__construct')) {
12. call_user_func_array(array($ob,'__construct'),$params);
13. }
Now, the problem is the constructor is called in line 09
But i want to call it manually at line 11-13
Is it possible? If then how? Any idea please?
If separating instantiation from initialization isn't strictly a requirement, there are two other possibilities: first, a static factory method.
Or, if you're using php 5.3.0 or higher, you can use a lambda:
The initialization method described by Asaph is great if for some reason you have a need to logically separate initialization from instantiation, but if supporting your use case above is a special case, not a regular requirement, it can be inconvenient to require users to instantiate and initialize your object in two separate steps.
The factory method is nice because it gives you a method to call to get an initialized instance. The object is initialized and instantiated in the same operation, though, so if you have a need to separate the two it won't work.
And lastly, I recommend the lambda if this initialization mechanism is uncommonly used, and you don't want to clutter your class definition with initialization or factory methods that will hardly ever be used.
to construct your object first and then pass parameters your could try this way:
then it is possible to construct the object and call
later.
I don't know if there are some security concerns by using the eval() method, but you could make yourself a function like this:
And now $ob should now be defined with its correct parameters, i haven't tested it and maybe there is a mistake in the code, but you get the idea....
In PHP you can create objects w/o calling the constructor. But that does not work by using new but by un-serializing an object instance.
The constructor can then be called manually.
Normally this is not needed. See as well: Loading an object from PHP session, does it call constructor?
It is not possible to prevent the constructor from being called when the object is constructed (line 9 in your code). If there is some functionality that happens in your
__construct()
method that you wish to postpone until after construction, you should move it to another method. A good name for that method might beinit()
.Why not just do this?
EDIT: I just thought of a hacky way you could prevent a constructor from being called (sort of). You could subclass
Test
and override the constructor with an empty, do-nothing constructor.This is might make sense if you're dealing with some code that you cannot change for some reason and need to work around some annoying behavior of a poorly written constructor.
Note that if the constructor (__construct method) contains arguments passed by reference, then the function:
will fail with an error.
I suggest you to use Reflection class instead; here is how you can do so: