Php 用于从方法创建新对象的漂亮语法

Php 用于从方法创建新对象的漂亮语法,php,coding-style,Php,Coding Style,有一个快捷方法可以从返回字符串的方法创建对象 目前,我用的是: class MyClass { /** * @return string */ public function getEntityName() { return 'myEntityName'; } } $myClassInstance = new MyClass(); // Need to get string $entityName = $myclassInstan

有一个快捷方法可以从返回字符串的方法创建对象

目前,我用的是:

class MyClass {

    /**
     * @return string
     */
    public function getEntityName() {
        return 'myEntityName';
    }
}

$myClassInstance = new MyClass();

// Need to get string
$entityName = $myclassInstance->getEntityName();

// And after I can instantiate it
$entity = new $entityName();

在PHP中,有获取字符串的快捷语法,但不用于从字符串创建对象。请参阅以下代码,其中我还包括一个“myEntityName”类:

<?php

class myEntityName {
    public function __construct(){
        echo "Greetings from " . __CLASS__,"\n";
    }
}
class MyClass {

    /**
     * @return string
     */
    public function getEntityName() {
        return 'myEntityName';
    }
}

$entityName = ( new MyClass() )->getEntityName();
$entity = new $entityName();

最后一条捷径接近我的需要<代码>$entityName=(新MyClass())->getEntityName()是php5.4的一个新特性。我需要/感兴趣的是简化实例化。谢谢。
$entityName = new ( ( new MyClass() )->getEntityName() );