Re: pointers in php
| From: | Richard Lynch | Date: | Fri, 24 Aug 2001 23:52:44 +0000 |
| Subject: | Re: pointers in php | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-64363@lists.php.net to get a copy of this message | ||
> I have a problem with pointers in php.
They're not pointers.
PHP is not C.
It happens to look C, but it's not.
> In the following code the parent object should be a pointer to class Main
> ($this), but it's a copy ...
It's an alias, not a pointer.
> <?php
> class Sub_A {
>
> // constructor
>
> function Sub_A(&$parent) {
>
> $this->parent = &$parent;
>
> }
>
> function Modify($text) {
>
> $this->parent->B->text = $text;
>
> }
>
> }
>
> class Sub_B {
>
> var $text = "untouched";
>
> // constructor
>
> function Sub_B(&$parent) {
>
> $this->parent = &$parent;
>
> }
>
> }
>
> class Main {
>
> // constructor
>
Style Tip:
Add var $A; var $B;
> function Main() {
>
> $this->A = new Sub_A($this);
I *think* you want &$this as the argument to do what you are trying to do...
>
> $this->B = new Sub_B($this);
>
> }
>
>
> }
>
> $test = new Main();
>
> $test->A->Modify('test');
>
> // this method should work, but doesnt (should return "test" instead of
> "untouched")
>
> echo $test->B->text."<br>";
>
> // parent isnt a pointer but a copy so absolute addressing works
>
> echo $test->A->parent->B->text."<br>";
>
> ?>
Bottom Line: It's not C. Don't expect it to work exactly like C.
--
WARNING richard@zend.com address is an endangered species -- Use
ceo@l-i-e.com
Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm