3

I have implemented method overriding in OOPS, but I'm not sure how method overriding is achieved in PHP. When you create a function with the same name, it will give you an error about the redeclaration of the function.

CodeWithCoffee
  • 1,896
  • 2
  • 14
  • 36
Lerry
  • 146
  • 7
  • method overriding is actually a class method overriding. If you have method `foo` in `someclass.php` and you want to overwrite that method, you have to create another class which extends `someclass.php` and override `foo` in new class. – Murtaza Khursheed Hussain Mar 05 '15 at 13:54
  • 1
    Method overloading cannot be achived in PHP, as you can pass default values in methods to bypass method overloading. – Murtaza Khursheed Hussain Mar 05 '15 at 13:55
  • It's doubtful whether the OP wanted to ask about method overloading or method overriding, but I've decided to be precise and answer the question literally. I'm going to expand my answer a bit to support the latter case as well. – Luka Mar 05 '15 at 14:18
  • The [Class Abstraction](http://php.net/manual/en/language.oop5.abstract.php) may be one option. – Seiji Manoan Mar 05 '15 at 15:12
  • possible duplicate of [What is function overloading and overriding in php?](http://stackoverflow.com/questions/2994758/what-is-function-overloading-and-overriding-in-php) – Seiji Manoan Mar 05 '15 at 15:13

1 Answers1

3

Method overriding in PHP is actually quite simple. You just specify the base class and then create a method (function) with the same name within the derived class.

class BaseClass {
  public function first_method() {
    echo("Hello! I am the base!\n");
  }
  public function do_something() {
    echo("Hi, there! I am the base!\n");
  }
}

class AnotherClass extends BaseClass {
  public function do_something() {
    echo("Hi, there! I am a derivative!\n");
  }
}

$base_class = new BaseClass();
$another_class = new AnotherClass();
$base_class->do_something();
$another_class->do_something();
$another_class->first_method();

Edit to Cover the Possible Question on Method Overloading :-)

In case you meant to ask about method overloading, then you should know it can't be done in PHP. There is another feature which can eventually give you the same result: default arguments. Here is a potential use case applicable to both, methods and functions:

function first_function($a, $b=NULL) {
  echo($a);
  if($b!==NULL) {
    echo($b);
  }
}

This is essentially the same as having two functions named first_function (in C++, for example), where each function has a different number of parameters, like this:

void first_function(int a) {
  cout << a << endl;
}

void first_function(int a, int b) {
  cout << a << endl;
  cout << b << endl;
}

It makes more sense to avoid conventional method overloading because PHP is a loosely typed language. Ending with two functions with same number of arguments would lead to a dead end because PHP interpreter wouldn't be able to determine which of these two functions you wanted to call, since there is no type sensitivity in PHP.

Luka
  • 1,718
  • 3
  • 19
  • 29
  • `echo` isn't a function, it's a language construct, so you don't need the parentheses. – ʰᵈˑ Mar 05 '15 at 13:56
  • 1
    I'm aware of that, but I still tend to use parentheses because of being used to functions like `printf` in the C programming language and its derivatives. :-) – Luka Mar 05 '15 at 14:05
  • It's cool `:)` Just pointing it out - +1'd this answer as it answers OPs question – ʰᵈˑ Mar 05 '15 at 14:07
  • Thanks. You actually made me do some research on this to see why exactly PHP developers decided to implement `echo` as a language construct instead of a function. ;-) – Luka Mar 05 '15 at 14:09
  • Actually you're not overriding any. Because it's just [Object Inheritance](http://php.net/manual/en/language.oop5.inheritance.php). – Seiji Manoan Mar 05 '15 at 14:55
  • @SeijiManoan wait what? It is overriding... Inheritance means u inherit all the public/protected members + methods, once u alter a method inside a child class its overriding – DarkBee Mar 05 '15 at 15:53
  • @DarkBee I got it. But, let's assume it's not "overriding" truly. Because the method is still unmodified. That solution only embeds one class with other one in order of joining their methods by hierarchy. You can't modify the method yet. If so, it wouldn't need any parent class then. All right... the final result matters... the object will be okay. – Seiji Manoan Mar 05 '15 at 16:42