In PHP, you cannot call an arbitrary method on a newly created object, for example new Foo()->someMethod();
Sorry, but as it is.
But you could create a job like this:
<?php class CustomConstructor { public static function customConstruct($methodName) { $obj = new static;
Extend CustomContructor as follows:
class YourClass extends CustomConstructor { public function someCoolMethod() {
And create them like this:
$foo = YourClass::customConstruct('someCoolMethod');
I have not tested it, but this or something like this should work.
Bugfix: this will only work in PHP 5.3 and later, as late static binding is required.
source share