Wednesday 20 March 2013

How to implement consturctor overloading in php


1 comment:

  1. Actually, PHP doesn't support method overloading the way C, C++ do. You can implement similar(not same) thing in a different way.

    You can pass a default value for a param. So what you can do it is use default for all the parameters, and then you will not necessarily need to pass them and they become optional.

    For example,
    I can have following thing for a constructor:

    function method123($arg1 = "abc", $arg2="bcd", $arg3="cde"){}

    This way, you can call the method four way:

    method123();
    method123($arg1);
    method123($arg1, $arg2);
    method123($arg1, $arg2, $arg3);

    But it is your job to identify the arguments and what you want to do for particular inputs inside your function.

    ReplyDelete