note 85219 modified in language.oop5 by yannick
| From: | yannick@php.net | Date: | Wed, 21 Jan 2009 05:57:44 +0000 |
| Subject: | note 85219 modified in language.oop5 by yannick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149573@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:
<?php
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.
?>
--was--
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.
?>
http://php.net/manual/en/language.oop5.php