note 85219 added to language.oop5
| From: | cincodenada_NO at SPAM_gmail dot com | Date: | Wed, 20 Aug 2008 23:37:08 +0000 |
| Subject: | note 85219 added to language.oop5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143692@lists.php.net to get a copy of this message | ||
kit dot lester:
Your terrible confusion was caused by a couple of errors in your test code.
Firstly, you had $c.makeme instead of $c->makeme, which screwed that up.
Secondly, inside the class methods, you used $killme instead of $this->killme. $this context is
required, otherwise the variables will just be local to the function, and not work as expected.
To wit:
<?
class C {
public $survivor = "s";
public $killme = "k";
}
$c = new C();
//echo $c."<p>";
// No longer echoes "Object #1" in PHP 5.2, throws an error instead
echo "/".$c->survivor."/".$c->killme."/<p>";
// That renders /s/k/ - as I expected.
unset($c->killme);
echo "/".$c->survivor."/".$c->killme."/<p>";
// That renders /s// - as the PHP
// programmer in would expect, but rather
// shocking to the C++-and-Java programmer
// in you.
// HAVING DISCOVERED THAT WORKS, I WONDERED IF THE
// CONVERSE WORKS... SO HERE WE GO...
$c->makeme = "m";
// No hint of a runtime error.
echo
"/".$c->survivor."/".$c->killme."/".$c->makeme."/<p>";
// Which renders
// /s//m/
// There was a typo in the orginal code, $c.makeme, which screwed everything up.
// Without the typo, everything works like it should in reverse.
//Therefore there is no inconsistency. However...
class D {
public $survivor = "s";
public $killme = "k";
function echo_killme()
{ echo "killme is ";
if (isset($this->killme)) echo "set to {$this->killme}";
else echo "unset";
echo ".<p>";
}
function echo_makeme()
{ echo "makeme is ";
if (isset($this->makeme)) echo "set to {$this->makeme}";
else echo "unset";
echo ".<p>";
}
function echo_survivor()
{ echo "survivor is ";
if (isset($this->survivor)) echo "set to {$this->survivor}";
else echo "unset";
echo ".<p>";
}
}
$d = new D();
$d->echo_killme();
// Renders
// killme is set to k.
unset($d->killme);
$d->echo_killme();
// Renders
// killme is unset.
$d->makeme = "m";
// No hint of a runtime error, creates on the fly
$d->echo_makeme();
// Renders
// makeme is set to m
//The above problems were caused by not using the $this->var syntax in the methods, as fixed
above.
?>
----
Server IP: 209.41.74.194
Probable Submitter: 70.91.202.233
----
Manual Page -- http://www.php.net/manual/en/language.oop5.php
Edit -- https://master.php.net/note/edit/85219
Del: integrated -- https://master.php.net/note/delete/85219/integrated
Del: useless -- https://master.php.net/note/delete/85219/useless
Del: bad code -- https://master.php.net/note/delete/85219/bad+code
Del: spam -- https://master.php.net/note/delete/85219/spam
Del: non-english -- https://master.php.net/note/delete/85219/non-english
Del: in docs -- https://master.php.net/note/delete/85219/in+docs
Del: other reasons-- https://master.php.net/note/delete/85219
Reject -- https://master.php.net/note/reject/85219
Search -- https://master.php.net/manage/user-notes.php