The output of this is "24", when I'd expect "44".
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();
Any ideas why my expectations are being shattered?
Your constructor isn't firing it needs to be called:
public function __construct(){
// constructor
}
otherwise the array fails to initialize.
Your question basically boils down to your line at the beginning of something()
, asking:
$this->array at this point is empty, why?
This is the case because PHP constructors need to be named __construct
, whereas yours is simply named construct
.
Your function construct()
is never called. You should name it __construct()
.