我想在数组中添加一个dog对象,然后var_dump
将数组保持为空。我是否打破了OOP概念的某些规则?
class Dog {
public $name;
public $bread;
}
class MyClass {
public $dogArr = [];
public function __construct( $key , callable $callback ) {
$dogArr[$key] = $callback ();
}
}
public function actionTest() {
$newDog = new \backend\components\MyClass ( "first" , function () {
$dog = new \backend\components\Dog();
$dog->name = "Archi";
$dog->bread = "Pomeran";
return $dog;
} );
var_dump ( $newDog->dogArr );
}
答案 0 :(得分:3)
您只需要对String configFileString = "";
File configFile = new File(getApplicationContext().getExternalFilesDir("/appointments"), "appointments.json");
try {
configFileString = getStringFromFile(configFile.toString());
JSONObject json = new JSONObject(configFileString);
JSONArray array = json.getJSONArray("appointments");
} catch (Exception e) {
e.printStackTrace();
}
构造函数进行一些小改动:
变化:
MyClass
到
$dogArr[$key] = $callback();
否则,您只是在局部变量中设置值,而不是类属性。