获取实体对象 json_encode

get to object of entity json_encode

我有一个数组:

$general_informations['company'] = $company_db

$company_db 是一个带有对象的实体,例如:

$city = $company_db->getCity();

现在我json_encode().

现在如何获取 javascript 中的对象 getCity()

喜欢:

var city = general_informations.company_db.getCity;
var city = general_informations.company_db.city;

json_encode 对对象创建 json,其中包括对象的所有 public 属性。 private protected 并且不包括所有方法

<?php
  class Sample {
    public $visibleProperty = true;
    protected $notVisibleProperty = false;
    private $invisibleProperty = false;
    public function getProperty(){
      return $notVisibleProperty;
    }
  }

  $test = new Sample();

  $json = json_encode($test); 
  echo $json; // {"visbleProperty": true}
?>

希望这有助于理解为什么您不能直接使用 js 访问您的方法