Php 获取扩展类的对象变量
我必须用json对Cane类(php)进行编码,问题是当我这样做时,json字符串结果没有Animale类属性。 它的结尾仅为:{“razza”:“n/d”}1 我如何拥有Animale class属性和Cane属性?Php 获取扩展类的对象变量,php,json,class,inheritance,Php,Json,Class,Inheritance,我必须用json对Cane类(php)进行编码,问题是当我这样做时,json字符串结果没有Animale类属性。 它的结尾仅为:{“razza”:“n/d”}1 我如何拥有Animale class属性和Cane属性? 守则: class Animale implements \JsonSerializable{ private $id; private $nome; protected function Animale($id=-1,$no
守则:
class Animale implements \JsonSerializable{
private $id;
private $nome;
protected function Animale($id=-1,$nome="n/d"){
$this->id=$id;
$this->nome=$nome;
}
public function jsonSerialize(){
return get_object_vars($this);
}
}
class Cane extends Animale implements \JsonSerializable{
private $razza;
function Cane($id=-1, $nome="n/d",$razza="n/d"){
parent::Animale($id, $nome);
$this->razza=$razza;
}
public function jsonSerialize(){
return get_object_vars();
}
}
请原谅我的英语;英语不是我的第一语言 尝试将
private
变量更改为protected
或public