[PHP] Tree-like structure in PHP?
I'm trying to use arrays to implement a sort of data tree. For the code to work, it is essential that the nodes can be edited after they have become part of the tree. That would require that the array elements are pushed by reference rather than by value. ?php $arr1 = array (); $arr2 = array (); array_push ($arr1, 1); array_push ($arr1, $arr2); // -- the important line array_push ($arr1, 2); array_push ($arr2, 3); array_push ($arr2, 4); // Contents of $arr2 are now empty // apparently $arr2 is inserted by value print_r ($arr1); echo Expected output:\n; $arr1[1] = $arr2; print_r ($arr1); ? What is the most obvious way to implement a tree-like structure in PHP? Is there a way to push arrays by reference rather than by value? BTW: I'm using PHP CLI version 5.2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Tree-like structure in PHP? (closed)
Thanks, the empasant helps! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Function variables in classes
Hi there, I'm trying to execute function variables. This works fine outside class code, but gives a fatal error when run within a class. The demo code is here: ?php function bar1 () { echo Yep, in bar1() right now\n; } function foo1 () { bar1(); $a = bar1; print_r ($a); echo \n; $a(); } class foobar { function bar2 () { echo Yep, in bar2() right now\n; } public function foo2 () { foobar::bar2(); $a = foobar::bar2; print_r ($a); echo \n; $a(); } } foo1(); $fb = new foobar (); $fb-foo2(); ? The error message reads: Fatal error: Call to undefined function foobar::bar2() in /home/paul/demo/demo.php on line 25 Is there anyone out there who can explain what's wrong in this code? Thanks, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Function variables in classes
I just did. The result is the same however Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Function variables in classes
Thanks, this helps. The code now works. In case this is truely a bug in PHP, where should I log it? Anything that I should do to make sure that it gets followed up? Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Function variables in classes
OK guys, thanks for all your inputs. Based on your guidance, I have tested the following code with a series of variations: class foobar { function bar2 () { echo Yep, in bar2() right now\n; } public function foo2 () { $a = bar2;// Experiment 0 $a(); // Fatal error } } And the variations: $a = bar2;// Experiment 0 $a(); // Fatal error: Call to undefined function bar2() $a = foobar::bar2;// Experiment 1 $a(); // Fatal error: Call to undefined function bar2() $a = bar2;// Experiment 2 eval($a.();); // Fatal error: Call to undefined function bar2() $a = foobar::bar2;// Experiment 3 eval($a.();); // Works but far from elegant $a = bar2;// Experiment 4 $this-$a();// Works fine $a = bar2;// Experiment 5 self::$a(); // Works fine So, I have a working solution right now. But I still don't understand the essence of the differences between experiment #1 and #4. Also, I don't understand the need to specify the class name in experiment #3, coming from #2. Functions bar2() and foo2() are part of the same class foobar, and I would assume that the name 'bar2' would be in scope of the function foo2. BTW: I'm running PHP v5.2.0-8 build and distributed by Debian (etch1). Thanks again and regards, Paul. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php