PHP、オブジェクトのポインタと配列の参照検証

class Test{
	public $var;
	function __construct($var){
		$this->var = $var;
	}
}
$testsO[] = new Test(1);
$testsO[] = new Test(2);
$testsO[] = new Test(3);

foreach ($testsO as $test){
		$test->var = -$test->var;
}
print_r($testsO);

$testsA[] = 1;
$testsA[] = 2;
$testsA[] = 3;

foreach ($testsA as &$test){
	$test = -$test;
}
unset($test);

print_r($testsA);

結果は
Array
(
    [0] => Test Object
        (
            [var] => -1
        )

    [1] => Test Object
        (
            [var] => -2
        )

    [2] => Test Object
        (
            [var] => -3
        )

)
Array
(
    [0] => -1
    [1] => -2
    [2] => -3
)

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です