xxxxxxxxxx
/*
1) You can use ReflectionClass and it's method newInstanceWithoutConstructor
introduced in PHP 5.4 Then it's very easy to create an instance of a
class without calling its constructor:
*/
$reflection = new ReflectionClass("ClassName");
$instance = $reflection->newInstanceWithoutConstructor(); //That's it!
/*
2) 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:
*/
class MyClass {
public __construct($file = null) {
if ($file) {
// perform whatever actions need to be done when $file IS set
} else {
// perform whatever actions need to be done when $file IS NOT set
}
// perform whatever actions need to be done regardless of $file being set
}
}
/*
3) Another option is to extend your class such that the constructor of the child class does not call the constructor of the parent class.
*/
class MyParentClass {
public __construct($file) {
// perform whatever actions need to be done regardless of $file being set
}
}
class MyChildClass extends MyParentClass {
public __construct() {
// perform whatever actions need to be done when $file IS NOT set
}
}