Method overloading and overriding in php

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Function overloading and overriding is the OOPs feature in PHP. In function overloading, more than one function can have same method signature but different number of arguments. But in case of function overriding, more than one functions will have same method signature and number of arguments. 
    Function Overloading: Function overloading contains same function name and that function performs different task according to number of arguments. For example, find the area of certain shapes where radius are given then it should return area of circle if height and width are given then it should give area of rectangle and others. Like other OOP languages function overloading can not be done by native approach. In PHP function overloading is done with the help of magic function __call[]. This function takes function name and arguments. 
    Example: 
     

    php

    Function Overriding: Function overriding is same as other OOPs programming languages. In function overriding, both parent and child classes should have same function name with and number of arguments. It is used to replace parent method in child class. The purpose of overriding is to change the behavior of parent class method. The two methods with the same name and same parameter is called overriding.
    Example: 
     

    php

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    Strictly speaking, there's no difference, since you cannot do either :]

    Function overriding could have been done with a PHP extension like APD, but it's deprecated and afaik last version was unusable.

    Function overloading in PHP cannot be done due to dynamic typing, ie, in PHP you don't "define" variables to be a particular type. Example:

    $a=1;
    $a='1';
    $a=true;
    $a=doSomething[];
    

    Each variable is of a different type, yet you can know the type before execution [see the 4th one]. As a comparison, other languages use:

    int a=1;
    String s="1";
    bool a=true;
    something a=doSomething[];
    

    In the last example, you must forcefully set the variable's type [as an example, I used data type "something"].

    Another "issue" why function overloading is not possible in PHP: PHP has a function called func_get_args[], which returns an array of current arguments, now consider the following code:

    function hello[$a]{
      print_r[func_get_args[]];
    }
    
    function hello[$a,$a]{
      print_r[func_get_args[]];
    }
    
    hello['a'];
    hello['a','b'];
    

    Considering both functions accept any amount of arguments, which one should the compiler choose?

    Finally, I'd like to point out why the above replies are partially wrong; function overloading/overriding is NOT equal to method overloading/overriding.

    Where a method is like a function but specific to a class, in which case, PHP does allow overriding in classes, but again no overloading, due to language semantics.

    To conclude, languages like Javascript allow overriding [but again, no overloading], however they may also show the difference between overriding a user function and a method:

    /// Function Overriding ///
    
    function a[]{
       alert['a'];
    }
    a=function[]{
       alert['b'];
    }
    
    a[]; // shows popup with 'b'
    
    
    /// Method Overriding ///
    
    var a={
      "a":function[]{
        alert['a'];
      }
    }
    a.a=function[]{
       alert['b'];
    }
    
    a.a[]; // shows popup with 'b'
    

    What is method overloading and overriding in PHP?

    Function overloading and overriding is the OOPs feature in PHP. In function overloading, more than one function can have same method signature but different number of arguments. But in case of function overriding, more than one functions will have same method signature and number of arguments.

    Is PHP support overloading and overriding?

    PHP does not support method overloading. In case you've never heard of method overloading, it means that the language can pick a method based on which parameters you're using to call it. This is possible in many other programming languages like Java, C++.

    What is method overriding in PHP?

    Method overriding allows a child class to provide a specific implementation of a method already provided by its parent class. To override a method, you redefine that method in the child class with the same name, parameters, and return type.

    Is there method overloading in PHP?

    Method Overloading: It is a type of overloading for creating dynamic methods that are not declared within the class scope. PHP method overloading also triggers magic methods dedicated to the appropriate purpose. Unlike property overloading, PHP method overloading allows function call on both object and static context.

    Chủ Đề