note 50276 deleted from language.oop5.typehinting by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 10:15:22 +0000 |
| Subject: | note 50276 deleted from language.oop5.typehinting by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196944@lists.php.net to get a copy of this message | ||
Note Submitter: caliban at darklock dot com
----
In case you're worried, type hinting does allow descendants. Extending the documentation
example:
<?php
// Example class
class MyClass
{
public function test(OtherClass $otherclass)
{
if(is_callable(array($otherclass,$otherclass->var)))
{
$otherclass->{$otherclass->var}();
}
else
{
echo $otherclass->var;
}
}
}
// Another example class
class OtherClass
{
public $var = 'Hello World';
}
// Yet another example class
class DerivedClass extends OtherClass
{
function __construct()
{
$this->var="Planet";
}
public function Planet()
{
echo "Hello ".$this->var;
}
}
$myclass = new MyClass;
$otherclass = new OtherClass;
$derivedclass = new DerivedClass;
// Works - prints "Hello World"
$myclass->test($otherclass);
// Works - calls DerivedClass::Planet()
// which prints "Hello Planet"
$myclass->test($derivedclass);
?>