note 50276 added to language.oop5.typehinting
| From: | caliban at darklock dot com | Date: | Wed, 23 Feb 2005 15:34:35 +0000 |
| Subject: | note 50276 added to language.oop5.typehinting | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85439@lists.php.net to get a copy of this message | ||
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);
?>
----
Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50276
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50276&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50276&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50276&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50276&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50276&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50276&report=yes
Search -- http://master.php.net/manage/user-notes.php