Bleadof’s world of tinkering

June 12, 2006

PHP4 OOP and references

Filed under: PHP,Programming,Tinkering — Bleadof @ 13:06

So today I decided check PHP4 OOP quirks. So now I know for sure how my code is going to behave. I wasn’t really sure how the PHP4 behaves with references so I did a test.

class ReferenceTest {

  var $reference;

  function ReferenceTest() {
    $this->reference = new Reference();
  }

  function getReference() {
    return $this->reference;
  }

  function &getReferenceReference() {
    return $this->reference;
  }
}

class Reference {

  var $var;

  function Reference() {
    $this->var = "";
  }

  function setVar($var) {
    $this->var = $var;
  }

  function getVar() {
    return $this->var;
  }

  function &getVarReference() {
    return $this->var;
  }

}

$rt =  new ReferenceTest();
$ref = $rt->getReference();
$ref->setVar("bar");
$ref2 =& $rt->getReferenceReference();
$ref2->setVar("baz");
$ref3 =& $rt->getReferenceReference();
$varRef =& $ref3->getVarReference();
$varRef = "baf";
$ref3->setVar("foo");

print "ref:".$ref->getVar()."<br/>";
print "ref2:".$ref2->getVar()."<br/>";
print "ref3:".$ref3->getVar()."<br/>";

Do you know what is the output? Here’s the anwser.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress