This is because your object (i.e. $thisin the internal context) will be copied by reference , not by value. To make a copy by value, you need to do:
public function add($type, $message)
{
$this->type = $type;
$this->message = $message;
self::$_alerts[] = clone $this;
}
As an alternative, you need to instantiate (for example, constructs such as new self- but cloneseem to be more flexible here) your object as many times as you want to copy.
, , . var_dump() print_r() - , . (.. ):
array(2) {
[0]=>
object(Alert)#1 (2) {
["type":"Alert":private]=>
string(5) "error"
["message":"Alert":private]=>
string(6) "test 2"
}
[1]=>
object(Alert)#1 (2) {
["type":"Alert":private]=>
string(5) "error"
["message":"Alert":private]=>
string(6) "test 2"
}
}
- , .