note 85168 added to language.oop5
| From: | kitdotlesteratportdotacdotuk at osu1 dot php dot net | Date: | Mon, 18 Aug 2008 18:21:30 +0000 |
| Subject: | note 85168 added to language.oop5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143605@lists.php.net to get a copy of this message | ||
<?
class C {
public $survivor = "s";
public $killme = "k";
}
$c = new C();
echo $c."<p>";
// That renders
// Object id #1
// - not documented, and rather handy to
// know for when one is debugging.
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 me.
// 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//Object id #1makeme/
// - which rather appalls all the programmers
// in me - the PHP programmer because he
// thinks it should it should work, and the
// C++/Java programmers because of the
// inconsistency with the unset working...
// Worse: looking at the view/source of what
// was rendered, the -> before makeme is
// missing. No doubt there is some contorted
// explanation of WHY it is missing, but
// that would be a justification for something
// that is undesirable because it will make
// it harder to debug assignments to mis-spelt
// member names.
// THE APPARENT FAILURE TO ASSIGN TO MAKEME, BUT WITH
// NO RUNTIME ERROR ON THE ASSIGNMENT, LEAVES ME
// WONDERING WHETHER IT SUCCEEDED, BUT NOT VISIBLY
// FROM OUTSIDE THE CLASS...
// ...so let's do a more elaborate class definition...
class D {
public $survivor = "s";
public $killme = "k";
function echo_killme()
{ echo "killme is ";
if (isset($killme)) echo "set to $killme";
else echo "unset";
echo ".<p>";
}
function echo_makeme()
{ echo "makeme is ";
if (isset($makeme)) echo "set to $makeme";
else echo "unset";
echo ".<p>";
}
}
$d = new D();
$d->echo_killme();
// Renders
// killme is unset.
// - OK.
$d->makeme = "m";
// No hint of a runtime error.
$d->echo_makeme();
// Renders
// makeme is unset.
// - so (apart from the asymmetry in the
// language) THE ABSENCE OF A RUNTIME
// ERROR IS ANOTHER DEBUGGING HAZARD WHEN
// ASSIGNING TO A MISSPELT MEMBER.
?>
----
Server IP: 69.147.83.197
Probable Submitter: 82.3.232.13
----
Manual Page -- http://www.php.net/manual/en/language.oop5.php
Edit -- https://master.php.net/note/edit/85168
Del: integrated -- https://master.php.net/note/delete/85168/integrated
Del: useless -- https://master.php.net/note/delete/85168/useless
Del: bad code -- https://master.php.net/note/delete/85168/bad+code
Del: spam -- https://master.php.net/note/delete/85168/spam
Del: non-english -- https://master.php.net/note/delete/85168/non-english
Del: in docs -- https://master.php.net/note/delete/85168/in+docs
Del: other reasons-- https://master.php.net/note/delete/85168
Reject -- https://master.php.net/note/reject/85168
Search -- https://master.php.net/manage/user-notes.php