How to send an object as paramater in a function call? I don't want to instantiate the object when passing it, so myFunction( new myObject() )
wouldn't work.
I want myFunction
to be able to instantiate the object later. I'm just not sure how to pass a class around in this manner.
You could pass a string and do:
myFunction($class) {
$object = new $class();
}
But a better OOP approach would be to pass a factory object and let the factory create the object:
class MyFactory {
public function create() {
return new myObject;
}
}
myFunction(MyFactory $factory) {
$object = $factory->create();
}
Pass the class name as a string, and instantiate it.
https://stackoverflow.com/a/4578343/362536
function ($className) {
$object = new $className();
}