PHPverse 2025

Voting

The Note You're Voting On

mitko at edabg dot com
16 years ago
<?php
/*
This example demonstrate how to use unknown variable arguments by reference.
func_get_args() don't return arguments by reference, but
debug_backtrace() "args" is by reference.
In PHP 5 this have no particular sense, because calling with arguments by reference
is depreciated and produce warning.
*/

class foo {

var
$bar = "default bar";

function
foo(/*variable arguments*/) {
// func_get_args returns copy of arguments
// $args = func_get_args();
// debug_backtrace returns arguments by reference
$stack = debug_backtrace();
$args = array();
if (isset(
$stack[0]["args"]))
for(
$i=0; $i < count($stack[0]["args"]); $i++)
$args[$i] = & $stack[0]["args"][$i];
call_user_func_array(array(&$this, 'bar'), $args);
}


function
bar($bar = NULL) {
if (isset(
$bar))
$this->bar = & $bar;
}
}

$global_bar = "bar global";
$foo = & new foo();
echo
"foo->bar: ".$foo->bar."</br>\n";
$foo->bar = "new bar";
echo
"global_bar: ".$global_bar."</br>\n";
/*
Result:
foo->bar: default bar</br>
global_bar: bar global</br>
*/

$foo = & new foo(&$global_bar);
echo
"foo->bar: ".$foo->bar."</br>\n";
$foo->bar = "new bar";
echo
"global_bar: ".$global_bar."</br>\n";
/*
Result:
foo->bar: bar global</br>
global_bar: new bar</br>
*/

?>

<< Back to user notes page

To Top