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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:56:42+00:00 2026-05-14T04:56:42+00:00

In PHP, how can a class reference its own name? For example, what would

  • 0

In PHP, how can a class reference its own name?

For example, what would the method look like to do this?

Dog::sayOwnClassName();
//echos "Dog";

Update

I see that everyone is saying get_class($this). But that’s not correct. That would work if I were creating an instance of Dog. I’m asking about calling a method of the Dog class itself. If Dog extends Mammal, then a call to get_class($this) inside the Dog class will return ‘Mammal.’.

In other words:

  • I’m not asking “what’s the class of the Dog class,” to which the answer is, “the Dog class is a member of the Mammal class.”
  • I’m also not asking “given an instance of Dog the dog class (called Rover), what is its class?”, to which the answer is “Dog.”
  • What I’m asking is, “can the Dog class itself tell me ‘my name is Dog?'”

For example:

class Mammal {    
  public function find_by_id($id){
    $query = "SELECT * FROM " . $myclass . " WHERE `id` = " . $id;
    //(etc)
    return $matching_object;
  }
}

class Dog extends Mammal {
//find_by_id method should know to do a SELECT from Dog table     
}

Update 2

Yacoby’s suggestion of get_called_class() was correct. Here’s how it works in the example I gave.

class Mammal {    
      public function find_by_id($id){
        $myclass = get_called_class();
        $query = "SELECT * FROM " . $myclass . " WHERE `id` = " . $id;
        //(etc)
        return $matching_object;
      }
    }

    class Dog extends Mammal {
    //find_by_id knows to do a SELECT from Dog table
    //and will return the correct dog object     
    }
  • 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-14T04:56:43+00:00Added an answer on May 14, 2026 at 4:56 am

    Three options, get_called_class(), get_class() or the magic constant __CLASS__

    Of those three get_called_class() is the one you want when dealing using a static function, although unfortuantely it does have a requirement of a PHP version of at least 5.3.0


    get_called_class()

    If you need to get the class in a static function when the class may be derived it is slightly different as self is resolved to the class name where it was placed (See Limitations of self::). To work around this issue you need to use the function from PHP 5.3.0 get_called_class().

    If you cannot use PHP 5.3.0 or greater you may find you cannot make the function static and still have it achieve the results you want.


    get_class()

    get_class() returns the name of the actual class that the object is, irrespective of where the function call is.

    class Animal{
         public function sayOwnClassName(){
             echo get_class($this);
         }
    }
    
    class Dog extends Animal{
    }
    
    
    $d = new Dog();
    $d->sayOwnClassName(); //echos Dog
    
    $a = new Animal();
    $a->sayOwnClassName(); //echos Animal
    

    get_class can also be used without a parameter, which would at first glance seem to indicate it would with static functions (as there is no need to pass $this), however when used without a parameter it works in the same way as __CLASS__

    class Animal{
         public static function sayOwnClassName(){
             echo get_class();
         }
    }
    
    class Dog extends Animal{
    }
    
    
    Dog::sayOwnClassName(); //echos Animal
    Animal::sayOwnClassName(); //echos Animal
    

    __CLASS__

    __CLASS__ always expands to the name of the class where __CLASS__ was resolved, even when inheritance is taken into account.

    class Animal{
         public function sayOwnClassName(){
             echo __CLASS__; //will always expand to Animal
         }
    }
    
    class Dog extends Animal{
    }
    
    $d = new Dog();
    $d->sayOwnClassName(); //echos Animal
    
    $a = new Animal();
    $a->sayOwnClassName(); //echos Animal
    

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

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can create a method for the dayrender event in… May 15, 2026 at 6:44 am
  • Editorial Team
    Editorial Team added an answer Use children() and each(), you can optionally pass a selector… May 15, 2026 at 6:44 am
  • Editorial Team
    Editorial Team added an answer Assuming the string is null terminated (you don't specify how… May 15, 2026 at 6:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.