对php对象如何处理数组感到困惑

Mat*_*hew 2 php

当我期望"44"时,它的输出是"24".

class some_class {
    public $array = array();

    public function construct() {
        $this->array[5] = 4;
    }

    public function something() {
        // $this->array at this point is empty, why?
        echo (isset($this->array[5])) ? $this->array[5] : 2;
        $this->array[5] = 4;
        // Here, $this->array holds the value 4 in the key 5 correctly
        echo (isset($this->array[5])) ? $this->array[5] : 2;
    }
}

$some_object = new some_class();
$some_object->something();
Run Code Online (Sandbox Code Playgroud)

有什么想法为什么我的期望被打破了?

Jak*_*kub 9

您的构造函数未触发它需要调用:

public function __construct(){
 // constructor
}
Run Code Online (Sandbox Code Playgroud)

否则数组无法初始化.