By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Fatal Error on Polymorphism

P: n/a
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;
}

}

$myClass = new aClass;
$myClass->aPrint('Kesavan');

$myClass2 = new aClass;
$myClass2->aPrint('Kesavan',24);
?>

/
************************************************** ************************************************** ****/
Kesavan M
m.*******@hotmail.com

Mar 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
..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
Mar 1 '07 #2

P: n/a
On Mar 1, 11:47 am, Michael Fesser <neti...@gmx.dewrote:
.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
Thanks Michael,

Your trick help's me..,
Isn't PHP fully OOP language/

Kesavan M
m.*******@hotmail.com

Mar 1 '07 #3

P: n/a
Kesavan wrote:
On Mar 1, 11:47 am, Michael Fesser <neti...@gmx.dewrote:
>.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

Thanks Michael,

Your trick help's me..,
Isn't PHP fully OOP language/
No, it isn't: go Java or C++ or something like that if you want 'real'
method overloading.
The support PHP gives for OOP is enough to get almost all tasks done.
Regards,
Erwin Moller
>
Kesavan M
m.*******@hotmail.com
Mar 1 '07 #4

P: n/a
..oO(Kesavan)
>Your trick help's me..,
Isn't PHP fully OOP language/
Method overloading is not necessarily a feature required to be an OOP
language. Usually you will find that feature in statically-typed
languages, where the compiler can decide which function to call by
looking at the types of the passed arguments. In dynamically-typed
languages it's much more difficult, if not impossible. I don't know if
there's any scripting language at all that supports that.

Micha
Mar 1 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.