note 97573 deleted from language.oop5.basic by cmb
| From: | cmb@php.net | Date: | Mon, 21 Nov 2016 18:15:40 +0000 |
| Subject: | note 97573 deleted from language.oop5.basic by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207269@lists.php.net to get a copy of this message | ||
Note Submitter: ben dot corne at gmail dot com
----
method calling context aware. By this I mean it will get treated differently while being in a new
statement compared to being in a regular call.
Example:
<?php
class Foo {
private $className = 'Bar';
public function make() {
return new $this->className();
}
public function callClassName() {
$this->className();
}
public function className() {
echo "foo\n";
}
};
class Bar {
public function hello() {
echo "bar\n";
}
};
$foo = new Foo();
$bar = $foo->make();
echo "expecting 'bar': ";
$bar->hello();
echo "expecting 'foo': ";
$foo->callClassName();
?>
even tough $this->className() is written two times in exactly the same way, the one contained in
a new statement gets the className field and the other performs the actual method.