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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex