}
for($i = 0; $i < 5;$i++) {
echo "Value:".$manyObjects[$i]->member."rn";
}
$obj = 1;
print_r($manyObjects);
?>
Hope this spares someone few hours of time ;-)
To henrik at newdawn dot dk, 28-Oct-2007
No, it's not strange.
$this->$var['key'] tries to look up 'key' in the array $var and then use that for the property name. $this->{$var}['key'], on the other hand...
I will describe how references interact with global variables.
Consider the following example. You create a DOM tree and then you use two functions. The first chooses an arbitrary tree element, while the second uses that element. Let us use a global variable as the means of communication between the two functions:
// global variable
var $dom_el;
test1(); // call first function
test2(); // call second function
function test1() {
global $dom_el, $Doc;
$dom_el = &$Doc->childNodes->item(0);
}
function test2() {
global $dom_el;
// $dom_el does not contain the reference! You cannot use it here.
// I am using PHP 4.4.6
}
If we assign a reference to the desired tree node in the first function then surprisingly the reference will disappear after the first function returns. That is, that reference is not saved in the global variable. Most likely, it is a feature of PHP 4 language. Specifically, I am using PHP 4.4.6. Thus, using a global variable to pass a reference from one function to another does not work.
However, if we save the reference as a field of an object, then the reference is preserved. Therefore, we need to wrap the global variable
in a class:
class Pointer {
var $i;
function Pointer() {
$this->i=null;
}
function set(&$_i) {
$this->i = &$_i;
}
function &get() {
return $this->i;
}
}
?>
Now the code looks like this:
$dom_el=new Pointer();
function test1() {
global $dom_el, $Doc;
$tmp = &$Doc->childNodes->item(0);
$dom_cur->set($tmp);
}
function test2() {
global $dom_el;
$tmp = &$dom_cur->get();
11/24 首页 上一页 9 10 11 12 13 14 下一页 尾页 |