如何收集PHP中具有相同或扩展类的所有对象?

时间:2018-12-10 09:20:40

标签: php wordpress

我正在开发一个WordPress插件,我想在其中连接不同的应用程序。我创建了一个“ App”类,并将“ App”类扩展为其他类。

现在,我想收集这些实例的所有对象,以便将它们列出。

有人可以帮助我吗?如果有兴趣,我什至愿意付钱给一位优秀的开发人员,他可以教我这一点以供将来开发。期待您的答复。

这是我当前的代码:

class App {

    public function __construct( $id, $name, $label, $description, $subscription_id ) {
        $this->id              = $id;
        $this->name            = $name;
        $this->label           = $label;
        $this->description     = $description;
        $this->subscription_id = $subscription_id;
    }
}
class App_Vimeo extends App {

    public function __construct( $id, $name, $label, $description, $subscription_id ) {
        $this->id              = $id;
        $this->name            = $name;
        $this->label           = $label;
        $this->description     = $description;
        $this->subscription_id = $subscription_id;
    }
}

class App_Facebook extends App {

    public function __construct( $id, $name, $label, $description, $subscription_id ) {
        $this->id              = $id;
        $this->name            = $name;
        $this->label           = $label;
        $this->description     = $description;
        $this->subscription_id = $subscription_id;
    }
}

class Get_Apps {

    public function __construct() {
        $this->get_apps();
    }

    public static function get_apps() {
        $apps   = array();
        $apps[] = new App_Vimeo( 1, 'vimeo', 'Vimeo', 'Vimeo app description', 1 );
        $apps[] = new App_Facebook( 1, 'facebook', 'Facebook', 'Facebook app description', 1 );
        return $apps;
    }
}

$apps = Get_Apps::get_apps();
var_dump( $apps );
`

1 个答案:

答案 0 :(得分:0)

与此有关:

$apps = Get_Apps::get_apps();
foreach($apps as $a) {
    echo get_class($a) . '<HR>';
}

get_class函数返回参数对象将其标识为的类的名称。