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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:08:18+00:00 2026-05-29T17:08:18+00:00

I have a class that sets up a class alias for other class names.

  • 0

I have a class that sets up a class alias for other class names. When a function is called inside of this class via the aliased class, I need to know which alias was used. Is there a way to do this in PHP?

I have tried the following code:

class foo
{
  public static function test()
  {
    var_dump(get_called_class());
  }
}

class_alias('foo', 'bar');

foo::test();
bar::test();

Which outputs:

string 'foo' (length=3)
string 'foo' (length=3)

But I want bar::test(); to output string 'bar' (length=3) instead. Grasping at straws, __CLASS__ and get_class() all produce the same result. I can’t seem to find anything else in the PHP documentation that would help me with this problem, but hopefully I am overlooking something.

How do you get the called aliased class when using class_alias?

  • 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-29T17:08:29+00:00Added an answer on May 29, 2026 at 5:08 pm

    You can’t do this easily as of PHP 5.3-5.5.

    There are only a handful of ways to determine what the “current” class is when a call is made. All of them return the unaliased class.

    class First {
        public function test1() { echo get_called_class(); }
        public function test2() { print_r(debug_backtrace()); }
        public function test3() { echo get_class($this); }
        public function test4() { echo __CLASS__; }
        public static function test5() { echo get_called_class(); }
    }
    class_alias('First', 'Second');
    $aliased = new Second();
    $aliased->test1(); // First
    $aliased->test2(); // array( 0 => array( ... [object] => First Object, ... ) )
    $aliased->test3(); // First
    $aliased->test4(); // First
    Second::test5();   // First
    

    3v4l demo.

    This is because class_alias doesn’t create a new class with the new name, it creates another entry in the list of classes that points at the same class as the original. When you ask PHP to look up what class is being used, it finds the original class, not the alias.

    If you need to create a new class is nothing more the original class with a different name, you’ll need to do so via inheritance. You can do this dynamically using eval. I can’t believe I’m going to recommend eval for something. Eww.

    class First {
        public function test1() { echo get_called_class(); }
        public function test2() { print_r(debug_backtrace()); }
        public function test3() { echo get_class($this); }
        public function test4() { echo __CLASS__; }
        public static function test5() { echo get_called_class(); }
    }
    
    function class_alias_kinda($original, $alias) {
        eval("class $alias extends $original {}");
    }
    
    class_alias_kinda('First', 'Second');
    $aliased = new Second();
    $aliased->test1(); // Second
    $aliased->test2(); // array( 0 => array( ... [object] => Second Object, ... ) )
    $aliased->test3(); // Second
    $aliased->test4(); // First (this is expected)
    Second::test5();   // Second
    

    3v4l demo.

    This might not work well for all cases. In PHP, private members can not be seen by descendant classes, so this might break your code horribly.

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

Sidebar

Related Questions

I know that Reflection can be expensive. I have a class that gets/sets to
I have a view controller class called PresidentsViewController that sets up data in a
I have a before_create callback that sets an error inside an object. class Animal
I have a class that contains two sets. They both contain the same key
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a Wicket page class that sets the page title depending on the
If i have a div with a class that sets the height 100px, and
I have an class definition with a __hash__ function that uses the object properties
i have simple singleton that sets pdo object from factory class , in the
I have a java class that sets an servlet attribute to a HashMap object:

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.