Problem with extending classes.

From: Date: Fri, 13 May 2005 10:09:50 +0000
Subject: Problem with extending classes.
Groups: php.general 
Request: Send a blank email to php-general+get-215128@lists.php.net to get a copy of this message
Hello, Been playing around with classes in php5, extending a base class and using a constructor etc.. But I seem to have a problem with getting the variables outside the class. In test1.txt, I cannot anything when I access the public variables for the class. In test2, when I make $contact into $contactcontact, I get the values for the variables, but it looks like all the variables in that class reference $contactcontact when you try to access them outside the class. Have I missed something or is there bugs with extending classes in php5? Does this happen in php4.3? Thanks for any help. Chris. <<test1.txt>> <<test2.txt>> ************************ This message has been delivered to the Internet by the Revenue Internet e-mail service *************************

<?php class baseclass { public $name; public $contact; function ksplit($text) { echo "<br>\nInside ksplit::"; $array = explode(':',$text); echo $text, " - ", $array[1]; return $array[1]; } } class extendedclass extends baseclass { public $address; function extendedclass($name, $contact, $address) { echo "Extendedclass Entry<br>\n"; echo $name, " - ", $contact, " <br>\n", $address; $this->$name = $this->ksplit($name); $this->$contact = $this->ksplit($contact); $this->$address = $this->ksplit($address); echo "<br>\nExtendedclass Exit<br>\n"; echo $this->$name, " - ", $this->$contact, " <br>\n", $this->$address; } } $myclass = new extendedclass("Name:Bob", "Contact:123456789", "Address:abcdefghijklmnopqrstuvwxyz"); echo "<br>\nOutside Class<br>\n"; echo $myclass->$name, " - ", $myclass->$contact, " <br>\n", $myclass->$address; ?> -----HTML RESULTS--- Extendedclass Entry Name:Bob - Contact:123456789 Address:abcdefghijklmnopqrstuvwxyz Inside ksplit::Name:Bob - Bob Inside ksplit::Contact:123456789 - 123456789 Inside ksplit::Address:abcdefghijklmnopqrstuvwxyz - abcdefghijklmnopqrstuvwxyz Extendedclass Exit Bob - 123456789 abcdefghijklmnopqrstuvwxyz Outside Class - <?php class baseclass { public $name; public $contactcontact; function ksplit($text) { echo "<br>\nInside ksplit::"; $array = explode(':',$text); echo $text, " - ", $array[1]; return $array[1]; } } class extendedclass extends baseclass { public $address; function extendedclass($name, $contact, $address) { echo "Extendedclass Entry<br>\n"; echo $name, " - ", $contact, " <br>\n", $address; $this->$name = $this->ksplit($name); $this->$contactcontact = $this->ksplit($contact); $this->$address = $this->ksplit($address); echo "<br>\nExtendedclass Exit<br>\n"; echo $this->$name, " - ", $this->$contactcontact, " <br>\n", $this->$address; } } $myclass = new extendedclass("Name:Bob", "Contact:123456789", "Address:abcdefghijklmnopqrstuvwxyz"); echo "<br>\nOutside Class<br>\n"; echo $myclass->$name, " - ", $myclass->$contactcontact, " <br>\n", $myclass->$address; ?> ------HTML RESULTS----- Extendedclass Entry Name:Bob - Contact:123456789 Address:abcdefghijklmnopqrstuvwxyz Inside ksplit::Name:Bob - Bob Inside ksplit::Contact:123456789 - 123456789 Inside ksplit::Address:abcdefghijklmnopqrstuvwxyz - abcdefghijklmnopqrstuvwxyz Extendedclass Exit Bob - 123456789 abcdefghijklmnopqrstuvwxyz Outside Class 123456789 - 123456789 123456789
« previous php.general (#215128) next »