note 85168 modified in language.oop5 by yannick
| From: | yannick@php.net | Date: | Wed, 21 Jan 2009 05:58:13 +0000 |
| Subject: | note 85168 modified in language.oop5 by yannick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149574@lists.php.net to get a copy of this message | ||
<?php
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.
?>
--was--
<?
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.
?>
http://php.net/manual/en/language.oop5.php