0

I’m new in PHP, and I want to do the same as the follow java source-code in PHP. Can anyone help me?

someMethod(int i) {
System.out.println("message");
// more code
}
someMethod(String s) {
System.out.println("another message");
// more different code
}
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
eightShirt
  • 1,457
  • 2
  • 15
  • 29
  • `You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists` taken from this post http://stackoverflow.com/questions/4697705/php-function-overloading – bansi May 13 '14 at 03:23
  • You can simply pass a null value as parameter.But when it comes to several parameters it'll be bit complex to understand. – CodeCanyon May 13 '14 at 03:24
  • it is perfectly valid in PHP to call this function `function someFunction(){}` like `someFunction($param1,$param2);`. Note the parameter count is more in the calling. – bansi May 13 '14 at 03:27

2 Answers2

3

You cannot overload PHP functions, as their signatures only include their name and not their argument lists: https://stackoverflow.com/a/4697712/386869

You can either do two separate functions (which I recommend) for what you're trying to do, or perhaps write one function and perform a different action for each type.

Writing two methods is straightforward:

function myFunctionForInt($param)
{
    //do stuff with int
}
function myFunctionForString($param)
{
    //do stuff with string
}

If you'd like to instead do it with one function and check the type (not really recommended):

function myFunction($param)
{
    if(gettype($param) == "integer")
    {
        //do something with integer
    }
    if(gettype($param) == "string")
    {
        //do something with string
    }
}

Docs for gettype()

I don't know that it's the best way. Also, for your particular example, Imat's example makes the most sense since you're just printing a message.

Community
  • 1
  • 1
muttley91
  • 12,278
  • 33
  • 106
  • 160
  • 1
    I suspect that eightShirt's actual use case involves more than just printing a message. But I believe that not knowing what the type of your expected parameters should be is bad coding practice, especially since PHP is loosely typed and will not complain if you accidentally assign an integer to a variable that was supposed to hold a string, for example. If you know you want to call `myFunction` with a string, then differentiate the function by naming it `myFunctionWithString` (or something similarly distinctive). – Brian Kendig May 13 '14 at 03:39
  • I agree with you there. That's why I mentioned it first, but figured it didn't really need explanation. I'll modify my answer to recommend that more strongly, though. – muttley91 May 13 '14 at 03:48
  • Thanks rar and @Brian-Kendig. Yes, I have more code. But the explanation helped me :) – eightShirt May 13 '14 at 03:51
1

Okay, so you want to create a function that prints a message.

function my_function_name()
{
   echo "your message here;
}

If you want a function with parameters, you can do this.

function my_function_name($params)
{
    echo "your message here";
    echo $params; //call the paramereters
}
Imat
  • 500
  • 2
  • 4
  • 15