Skip to main content

function overloading and overriding in php


In php what do you mean by function overloading and function overriding. and what is the difference between both of them? couldn't figure out what is the difference between them.



Thanks in advance for any help


Source: Tips4allCCNA FINAL EXAM

Comments

  1. In Object Oriented Programming (OOP), Overloading is defining functions that have similar signatures, yet have different parameters. Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that function.

    In PHP, you can only overload methods using the magic method __call.

    An example of overriding:

    <?php

    class Foo {
    function myFoo() {
    return "Foo";
    }
    }

    class Bar extends Foo {
    function myFoo() {
    return "Bar";
    }
    }

    $foo = new Foo;
    $bar = new Bar;
    echo($foo->myFoo()); //"Foo"
    echo($bar->myFoo()); //"Bar"
    ?>

    ReplyDelete
  2. Function overloading and overriding are two different concepts...

    Function overloading occurs when you define the same function name twice (or more) using different set of parameters. For example:

    class Addition {
    function compute($first, $second) {
    return $first+$second;
    }

    function compute($first, $second, $third) {
    return $first+$second+$third;
    }
    }


    In the example above, the function compute is overloaded with two different parameter signatures. *This is not yet supported in PHP. An alternative is to use optional arguments:

    class Addition {
    function compute($first, $second, $third = 0) {
    return $first+$second+$third;
    }
    }




    Function overriding occurs when you extend a class and rewrite a function which existed in the parent class:

    class Substraction extends Addition {
    function compute($first, $second, $third = 0) {
    return $first-$second-$third;
    }
    }


    For example, compute overrides the behavior set forth in Addition.

    ReplyDelete
  3. Strictly speaking, there's no difference, since you cannot do neither :)

    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'

    ReplyDelete
  4. PHP 5.x.x does not support overloading this is why PHP is not fully OOP.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.