是否可以在扩展类中使用 类实例 调用静态方法?在扩展它时,我不知道从我的课程中获得$this->access
来自__construct()
的{{1}},所以也许PHP专家可以帮助我。
namespace Folder\Subfolder;
class ClassA { // <-- this class is a custom helper
public static function accessA() {
return 'Class A\'s Access';
}
public static function accessB() {
return 'Class B\'s Access';
}
}
在其他文件中:
use Folder\SubFolder\ClassA;
class ParentClass {
public function __construct() {
$this->access = new \Folder\Subfolder\ClassA;
}
public function admin() {
$ac = $this->access::accessA();
//When I do this it gives me an error syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
$ac = $this->access->accessA();
//When I also do this it gives me an error Call to a member function accessA() on null
var_dump($ac);
exit;
}
}
class ChildClass extends ParentsClass {
public function __construct() {
parent::__construct();
}
public function employee() {
$ac = $this->access::accessB();
var_dump($ac);
exit;
}
}
class ChildClass2 extends ParentClass {
public function __construct() {
parent::__construct();
}
} //And so on and so forth
注意:我不想使用new
关键字来实例化另一个类对象。
我想仅使用此代码$this->access;