In the following script I declare two methods with the same name calculate but different signatures. But I get an error on declaration: . Why is that ? Php doesn’t support this type of polymorphism ?
Fatal error: Cannot redeclare Tester::calculate() in /opt/lampp/htdocs/tester.php on line 7
<?php
class Tester {
public function calculate() {
echo 2*2;
}
public function calculate($var_1,$var_2) {
echo $var_1*$var_2;
}
}
$obj = new Tester();
$obj->calculate();
echo "<br />";
$obj->calculate(5,4);
PHP != (Java, C++, C#, VB.Net)
Although PHP borrows heavily from Java, there are some big differences between the two (and other OOP languages). One big one is you can’t have methods using the same names with different parameters. You either need to: