PHP:在子类中使用className

时间:2015-12-09 09:43:16

标签: php oop

---- file 1 ----
use Models\Post;
class A 
{
    public function fct() {
        Post::all();
    }
}

---- file 2 ----
class B extends A {

}

如何在 B类中使用Post而无需再次写入使用Models \ Post

1 个答案:

答案 0 :(得分:0)

尝试这种方式

---- file 1 ----
use Models\Post as Post;
class A 
{
    public $post;
    // instantiate in __construct and store it to a public or protected variable

    public function __construct() {
       $this->post = new Post;
    }
    public function fct() {
        $this->post->all();
    }
}

---- file 2 ----
class B extends A {

}