Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取从PHP发送的JSON对象的类名_Javascript_Php_Json_Angularjs - Fatal编程技术网

Javascript 获取从PHP发送的JSON对象的类名

Javascript 获取从PHP发送的JSON对象的类名,javascript,php,json,angularjs,Javascript,Php,Json,Angularjs,我有一个PHP脚本,它在站点上加载用户的操作,并通过$http服务从AngularJS查询POST方法 json\u编码相当简单,不使用接口JsonSerializable,只使用json\u编码($object,json\u PRETTY\u PRINT)。但是,当在页面上显示动作时,我需要能够知道这些动作是什么类。我的动作类是这样的: class PostComment extends Action{}; class PostThread extends Action{}; //php c

我有一个PHP脚本,它在站点上加载用户的操作,并通过
$http
服务从AngularJS查询<代码>POST方法

json\u编码
相当简单,不使用
接口JsonSerializable
,只使用
json\u编码($object,json\u PRETTY\u PRINT)
。但是,当在页面上显示动作时,我需要能够知道这些动作是什么类。我的动作类是这样的:

class PostComment extends Action{};
class PostThread extends Action{};
//php code
$object->className = 'MyClass';

然而,在我从PHP发送的JSON数据中,我看不到PHP中类的发送位置(我认为所有JSON数据都在类似于
StdClass
?)的一般类下)。除了分配一个属性
$this->type
并从中计算类之外,我如何获得从PHP发送的JSON对象的类(使用Javascript或一些
angular.
方法)

确实,JSON中没有类。只需使用您喜欢的任何名称将类名添加到JSON中,并在前端使用它。 所以会是这样的:

class PostComment extends Action{};
class PostThread extends Action{};
//php code
$object->className = 'MyClass';
将产生

  {
     "className":"MyClass",
     "blabla":"foo",
     ....
  }

json序列化没有通用类哦,你说得对,我可以在jsonSerialize方法中添加类名。php代码应该类似于
$this->className=get\u class($this)内部初始化方法。它将更加灵活。请不要让$this->className成为公共属性。我认为更好的做法是
实现JsonSerializable
,然后指定一些方法来返回用于json编码的正确数组,并在
jsonSerialize
方法中,使用类似于
$this->className=get\u class($this)的内容指定类名