Posted by Michael Fesser on 03/01/07 06:47
..oO(Kesavan)
>I need polymorphism in php.
>When I run this following code I get the following error.
>Fatal error: Cannot redeclare aClass::aPrint() in C:\Program Files
>\xampp\htdocs\k7\prCls.php on line 16
>
>How I rectify it?
>
>
><?php
>
> class aClass{
>
> function aPrint($name){
> echo $name;
> }
>
> function aPrint($name,$age){
> echo $name;
> echo $age;
> }
>[...]
PHP doesn't support that. But you could make $age an optional argument
and check inside the function whether it was passed or not:
function aPrint($name, $age = NULL) {
print $name;
print !is_null($age) ? $age : '';
}
Micha
Navigation:
[Reply to this message]
|