php后期静态绑定修订错误,需要T_函数

php后期静态绑定修订错误,需要T_函数,php,late-static-binding,Php,Late Static Binding,我是OOP新手,我一直在研究这个例子,但我似乎无法摆脱这个错误 分析错误:语法错误,意外“;”,在第16行的C:\Program Files(x86)\Apache Software Foundation\Apache2.2\…\php\u late\u static\u bindings.php中需要T_函数 我试图执行以下代码: abstract class father { protected $lastname=""; protected $gender="";

我是OOP新手,我一直在研究这个例子,但我似乎无法摆脱这个错误

分析错误:语法错误,意外“;”,在第16行的C:\Program Files(x86)\Apache Software Foundation\Apache2.2\…\php\u late\u static\u bindings.php中需要T_函数

我试图执行以下代码:

abstract class father {
    protected $lastname="";
    protected $gender="";

    function __construct($sLastName){
        $this->lastname = $sLastName;
    }

    abstract function getFullName();

    public static function create($sFirstName,$sLastName){
        return new self($sFirstName,$sLastName);
    };
}

class boy extends father{
    protected $firstname="";

    function __construct($sFirstName,$sLastName){
        parent::__construct($sLastName);
        $this->firstname = $sFirstName;
    }

    function getFullName(){
        return("Mr. ".$this->firstname." ".$this->lastname."<br />");
    }
}

class girl extends father{
    protected $firstname="";

    function __construct($sFirstName,$sLastName){
        parent::__construct($sLastName);
        $this->firstname = $sFirstName;
    }

    function getFullName(){
        return("Ms. ".$this->firstname." ".$this->lastname."<br />");
    }

}


$oBoy = boy::create("John", "Doe");
print($oBoy->getFullName());
抽象类父类{
受保护的$lastname=“”;
受保护$性别=”;
函数构造($sLastName){
$this->lastname=$sLastName;
}
抽象函数getFullName();
公共静态函数create($sFirstName,$sLastName){
返回新的self($sFirstName,$sLastName);
};
}
班上的男孩比他的父亲长{
受保护的$firstname=“”;
函数构造($sFirstName,$sLastName){
父项::_构造($sLastName);
$this->firstname=$sFirstName;
}
函数getFullName(){
返回(“先生”。$this->firstname。”“$this->lastname。”
”); } } 班上的女孩比父亲长{ 受保护的$firstname=“”; 函数构造($sFirstName,$sLastName){ 父项::_构造($sLastName); $this->firstname=$sFirstName; } 函数getFullName(){ 返回(“女士”。$this->firstname。”“$this->lastname。”
”); } } $oBoy=男孩::创建(“约翰”,“多伊”); 打印($oBoy->getFullName());
有人有什么想法吗? $oGirl=girl::创建(“Jane”、“Doe”);
打印($oGirl->getFullName())

PHP的错误报告通常非常好。只要读一下错误。问题在于:

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
};
删除训练分号

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
}

PHP的错误报告通常非常好。只要读一下错误。问题在于:

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
};
删除训练分号

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
}

首先必须删除方法定义后的分号

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
} // there was a semi-colon, here

然后,您可能想使用
静态
,而不是
自我
,这里:

public static function create($sFirstName,$sLastName){
    return new static($sFirstName,$sLastName);
}
说明:

  • self
    指向编写它的类——这里是
    父类,它是抽象的,不能实例化
  • 另一方面,
    static
    ,意味着后期的静态绑定——这里,将指向您的
    boy
    类;哪一个是您要实例化的

首先必须删除方法定义后面的分号

public static function create($sFirstName,$sLastName){
    return new self($sFirstName,$sLastName);
} // there was a semi-colon, here

然后,您可能想使用
静态
,而不是
自我
,这里:

public static function create($sFirstName,$sLastName){
    return new static($sFirstName,$sLastName);
}
说明:

  • self
    指向编写它的类——这里是
    父类,它是抽象的,不能实例化
  • 另一方面,
    static
    ,意味着后期的静态绑定——这里,将指向您的
    boy
    类;哪一个是您要实例化的

返回新的static
应该是调用。因为否则它将尝试实例化抽象类(将失败)。。。但是第16行是什么?
应该调用returnnewstatic
。因为否则它将尝试实例化抽象类(将失败)。。。但是第16行是什么?好吧,解决了这个问题,我不敢相信我错过了这个问题,我不敢相信我错过了+1,但请记住,后期静态绑定将只在PHP 5.3中起作用:)我删除了分号,将self改为static,我现在收到错误解析错误:语法错误,意外的T_static,第14Heu行的C:\Program Files(x86)\Apache Software Foundation\Apache2.2\htdocs\brjulias\u dev\php\u late\u static\u bindings.php中应为T\u字符串或T\u变量或“$”。。。您确定使用的是PHP>=5.3吗?PHP<5.3时不存在后期静态绑定是的,它存在:-)(不要忘记读取)+1,但请记住,后期静态绑定仅在PHP 5.3时起作用:)我删除了分号并将self更改为Static,我现在收到错误解析错误:语法错误,意外的t_Static,在C:\Program文件(x86)中应为t_字符串或t_变量或“$”\ApacheSoftwareFoundation\Apache2.2\htdocs\brjulias\u dev\php\u late\u static\u bindings.php第14Heu行。。。您确定使用的是PHP>=5.3吗?PHP<5.3不存在后期静态绑定是的,它存在:-)(不要忘记阅读)