首页 文章

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

提问于
浏览 887
-1

我正在开发一个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

    顺其自然:

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

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

相关问题