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

  • SEARCH
  • Home
  • 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 8308305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:46:09+00:00 2026-06-08T18:46:09+00:00

Basically my question is as stated in the title… I want to give users

  • 0

Basically my question is as stated in the title…

I want to give users the ability to define an alias for the static methods in a class (in my case specifically for MyClass).

I haven’t found anything similar in function to class_alias. Sure, the user could just define their own function that calls the static method to achieve this goal… but are there other/better/simpler/different ways to do this?

Here is my attempt so far…

<?php
class MyClass {

    /**
     * Just another static method.
     */
    public static function myStatic($name) {
        echo "Im doing static things with $name :)";
    }

    /**
     * Creates an alias for static methods in this class.
     *
     * @param   $alias      The alias for the static method
     * @param   $method     The method being aliased
     */
    public static function alias($alias, $method) {
        $funcName = 'MyClass::'.$method;    // TODO: dont define class name with string :p
        if (is_callable($funcName)) {
            $GLOBALS[$alias] = function() use ($funcName){
                call_user_func_array($funcName, func_get_args());
            };
        }
        else {
            throw new Exception("No such static method: $funcName");
        }
    }
}

MyClass::alias('m', 'myStatic');
$m('br3nt');  

Also, feel free to comment on any pros or cons in my approach that I may not have considered. I understand that there are some risks in this approach, such as, the aliased variable could be overridden after the user has defined it.

  • 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-06-08T18:46:11+00:00Added an answer on June 8, 2026 at 6:46 pm

    Perhaps you can make use of the __callStatic “magic” method. See here for details.

    I’m not sure how you plan to map between the aliases and the actual static methods, though. Maybe you can have a configuration XML where you specify the mapping and you forward the calls to the real method with __callStatic based on that.

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

Sidebar

Related Questions

I have a simple question about creating multiple initialisers within an objective-c class. Basically
The title basically says it all. Suppose I have an element which I want
Basically the question is how do I run gpgpu code in a Metro app.
Basically my question is the exact same one as this: Simple client/server, TCP/IP encrypting
Basically the question could more probably be more accurately asked, how do I simply
Basically the question is, Why are NONE of the changes I've been making in
Basically my question is i have a website which was developed around 3 years
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
This is basically the same question as Click items in select box, and then
I can provide more detail if necessary, but my question is basically thus: If

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.