在静态方法(PHP)中从当前类创建对象
我的类模型和其他类存在一些问题,因此我用这个简单的示例来解释我的问题:在静态方法(PHP)中从当前类创建对象,php,class,oop,inheritance,Php,Class,Oop,Inheritance,我的类模型和其他类存在一些问题,因此我用这个简单的示例来解释我的问题: class person{ public static $a = "welcome"; public function __construct(){ } public static function getobject() { $v = new person(); return $v; } } class st
class person{
public static $a = "welcome";
public function __construct(){
}
public static function getobject()
{
$v = new person();
return $v;
}
}
class student extends person{
public static $b = "World";
}
$st = student:getobject();//this will return person object but I want student object
echo $st->$b; // There is an error here because the object is not student
所以我想知道该写什么,而不是$v=新人;获取上一个继承类的对象。使用的static关键字
因此,使用student::getobject可以获得student的一个实例
检索静态数据,但为什么$b适当的话,你可以做$st:$b,或者简单地做学生:$b。使用的静态关键字
因此,使用student::getobject可以获得student的一个实例
检索静态数据,但为什么$b适当的话,你可以做$st:$b,或者简单的学生::$b。ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh,因为我现在不能这么做:因为网站规则,我会因为这个PHP而死,静态在这里做什么,静态和类名之间的关系是什么,无论如何,谢谢,它正在工作,我会在5分钟后将你的答案标记为正确答案,因为我现在不能这么做:因为网站规则
public static function getobject()
{
$v = new static();
return $v;
}