可调用的结果无法分配给数组

时间:2017-11-27 14:26:04

标签: php

我想在数组中添加一个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 );
}

1 个答案:

答案 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();

否则,您只是在局部变量中设置值,而不是类属性。