Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:51:04+00:00 2026-05-27T08:51:04+00:00

Can I have two methods sharing the same name, but with different arguments? One

  • 0

Can I have two methods sharing the same name, but with different arguments?

One would be public static and would take 2 arguments, the other one just public and takes only one argument

example

class product{

  protected
    $product_id;

  public function __construct($product_id){
    $this->product_id = $product_id;
  }

  public static function getPrice($product_id, $currency){
    ...
  }

  public function getPrice($currency){
    ...
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:51:05+00:00Added an answer on May 27, 2026 at 8:51 am

    I’m just giving you the super lazy option:

    function __call($name, $args) {
        $name = $name . "_" . implode("_", array_map("gettype", $args)));
        return call_user_func_array(array($this, $name), $args);
    }
    

    That would for example invoke the real function name getPrice_string_array for two parameters of that type. That’s sort of what languages with real method signature overloading support would do behind the scenes.

    Even lazier would be just counting the arguments:

    function __callStatic($name, $args) {
        $name = $name . "_" . count($args);
        return call_user_func_array(array($this, $name), $args);
    }
    

    That would invoke getPrice_1 for 1 argument, or getPrice_2 for, you guessed it, two arguments. This might already suffice for most use cases. Of course you can combine both alternatives, or make it more clever by search for all alternative real method names.

    If you want to keep your API pretty and user-friendly implementing such elaborate workarounds is acceptable. Very much so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two methods with the same name, for different purposes in 2 different
Can somebody explain me one thing. I have two methods in my controller :
I have two methods in the same class and would like to find out
I have two methods: power and factorial: public static long pow(int x, int n)
I have two methods that do the same thing, one works with a Dictionary<object,
I have two static methods that I want to use for error handling. One
I have the following two methods that (as you can see) are similar in
In Winforms you can have two controls tied to the same datasource in a
I've got a table recording views of programs. Each program can have two different
I have two ASP.NET sites (they can not run in the same process) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.