Last active
January 3, 2016 01:49
-
-
Save windix/8391492 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function change_value_1(array $o) { | |
$o['b'] = 5; | |
} | |
function change_value_2(array $o) { | |
$o = array(); | |
$o['b'] = 5; | |
} | |
function change_value_3(array &$o) { | |
$o = array(); | |
$o['b'] = 5; | |
} | |
function test($func_name) { | |
$o = array('a' => 1, 'b' => 2); | |
call_user_func($func_name, $o); | |
print_r($o); | |
} | |
test('change_value_1'); | |
// => array('a' => 1, 'b' => 2); | |
test('change_value_2'); | |
// => array('a' => 1, 'b' => 2); | |
//test('change_value_3'); | |
// this will result a warning "Parameter 1 to change_value_3() expected to be a reference, value given" | |
$o = array('a' => 1, 'b' => 2); | |
change_value_3($o); | |
print_r($o); | |
// => array('b' => 5); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Obj { | |
public $value; | |
public function __construct() { | |
$this->value = 1; | |
} | |
} | |
function change_value_1(Obj $o) { | |
$o->value = 5; | |
} | |
function change_value_2(Obj $o) { | |
$o = new Obj(); | |
$o->value = 5; | |
} | |
function change_value_3(Obj &$o) { | |
$o = new Obj(); | |
$o->value = 5; | |
} | |
function test($func_name) { | |
$o = new Obj(); | |
call_user_func($func_name, $o); | |
echo $o->value . "\n"; | |
} | |
test('change_value_1'); | |
// => 5 | |
test('change_value_2'); | |
// => 1 | |
//test('change_value_3'); | |
// this will result a warning "Parameter 1 to change_value_3() expected to be a reference, value given" | |
$o = new Obj(); | |
change_value_3($o); | |
echo $o->value . "\n"; | |
// => 5 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment