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 693283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:44:17+00:00 2026-05-14T02:44:17+00:00

I have a couple of functions inside a class that essentially do the same

  • 0

I have a couple of functions inside a class that essentially do the same thing:

public function fn_a(){
        return __FUNCTION__;
}
public function fn_b(){
        return __FUNCTION__;
}
public function fn_c(){
        return __FUNCTION__;
}

I need those functions to remain in their current names so I intentionally did not do:

public function fn_($letter){
        return __FUNCTION__.$letter;
}

I was hoping for some sort of way to minify the verboseness of code here, since they all do the same. The ultimate situation would be something like this:

public functions fn_a, fn_b, fn_c() {
      return __FUNCTION__;
}

Another solution, if applicable, might be doing something like Class’s “extends”:
fn_b, fn_c extend fn_a?

What do you think guys?

  • 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-14T02:44:17+00:00Added an answer on May 14, 2026 at 2:44 am

    Any syntax like the one you suggested is not possible : if you want several distinct functions, you have to declare all those functions.

    Still, a possibility could be that your fn_a, fn_b and fn_c functions just be simple wrappers arround a more complex one :

    public function fn_a(){
        return fn_all('a');
    }
    public function fn_b(){
        return fn_all('b');
    }
    public function fn_c(){
        return fn_all('c');
    }
    
    public function fn_all($name) {
        // a lot of complex stuff here
    }
    

    With that, depending on the length on the fn_all function, of course, you would reduce the amount of code-duplication.


    Another idea (not sure how this could be done with methods, so I’ll demonstrate with functions) would be to use Closures — which means PHP >= 5.3

    The basic idea being that you’d have a first function, that would return another one — which would bind the parameter passed to the first one :

    First, the function that creates the others :

    function creator($name) {
        return function () use ($name) {
            return $name;
        };
    }
    

    And, then, let’s get three functions, using that creator one :

    $fn_a = creator('a');
    $fn_b = creator('b');
    $fn_c = creator('c');
    

    And now, calling those three functions :

    echo $fn_a() . '<br />';
    echo $fn_b() . '<br />';
    echo $fn_c() . '<br />';
    

    We get the following output :

    a
    b
    c
    

    I’ve never good at explaining how anonymous functions and closures work — but searching for “closure” on google should help you understand ; note that you can read tutorial about closures in Javascript : the idea is exactly the same.

    (And, as closures are new in PHP — arrived with PHP 5.3 — you will not find as many tutorials as for Javascript)

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

Sidebar

Related Questions

I have a couple questions. Are all functions inside of a class member functions?
I have a couple of functions that loop around the surrounding cells of a
I have some code in a couple of different functions that looks something like
INTRODUCTION : I have a function(callback) that receives a object as an argument, inside
I have some functions that use curl to pull information off a couple of
I have a couple of functions that perform HTTP POST/GET/HEAD requests. For the POST
So I have a couple of functions that work with a string type I
I'll have couple of python functions I must interface with from the assembly code.
I have the following couple of C pre-processor macros for creating test functions: //
I have couple resource DLLs that I currently load when application starts using following

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.