By any means, is it possible to create an instance of a PHP class without calling its constructor?
I have Class A and while creating an instance of it, I’m passing a file and in the constructor of Class A I’m opening that file.
Now in Class A, there is a function which I need to call, but I don’t have to pass a file, so there is no need for the constructor functionality of opening a file as none is being passed.
So my question is: Is it by any means possible to create an instance of a PHP class without calling its constructor?
Note: I cannot make a function static as I’m using some of the class properties in a function.
A classes constructor will always be called. There are a couple ways you could work around this, though.
The first way is to provide default values for your parameters in the constructor, and only perform certain actions on those parameters if they’re set. For example:
Another option is to extend your class such that the constructor of the child class does not call the constructor of the parent class.