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
}
3
  • You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists taken from this post stackoverflow.com/questions/4697705/php-function-overloading Commented May 13, 2014 at 3:23
  • You can simply pass a null value as parameter.But when it comes to several parameters it'll be bit complex to understand. Commented May 13, 2014 at 3: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. Commented May 13, 2014 at 3:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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).
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.
Thanks rar and @Brian-Kendig. Yes, I have more code. But the explanation helped me :)
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
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.