note 85904 modified in function.array-splice by danbrown

From: Date: Thu, 12 Feb 2009 22:17:39 +0000
Subject: note 85904 modified in function.array-splice by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-150596@lists.php.net to get a copy of this message
Note: If replacement is not an array, it will be typecast to one (i.e. (array) $parameter). This may result in unexpected behavior when using an object replacement . Example : <?php class A() { private $a; private $b; public function __construct() { $this->a = "foo"; $this->b = "bar"; } } $array = array(); array_splice($array, 0, 0, new A()); print_r($array); ?> Outputs : Array : Array { [0] => foo [1] => bar } Solution : Enforce the array() on the object. <?php array_splice($array, 0, 0, array(new Object()); ?> Source : http://bugs.php.net/bug.php?id=44485 --was-- Note: If replacement is not an array, it will be typecast to one (i.e. (array) $parameter). This may result in unexpected behavior when using an object replacement . Example : class A() { private $a; private $b; public function __construct() { $this->a = "foo"; $this->b = "bar"; } } $array = array(); array_splice($array, 0, 0, new A()); print_r($array); Outputs : Array : Array { [0] => foo [1] => bar } Solution : Enforce the array() on the object. array_splice($array, 0, 0, array(new Object()); Source : http://bugs.php.net/bug.php?id=44485 http://php.net/manual/en/function.array-splice.php

« previous php.notes (#150596) next »