title: PHP: how can a class reference its own name?
body: <p>In PHP, how can a class reference its own name?</p>
  
  <p>For example, what would the method look like to do this?</p>
  
  <pre><code>Dog::sayOwnClassName();
  //echos "Dog";
  </code></pre>
  
  <h2>Update</h2>
  
  <p>I see that everyone is saying <code>get_class($this)</code>. But that's not correct. That would work if I were creating an <strong>instance</strong> of Dog. I'm asking about calling a method of the Dog class itself. If <code>Dog</code> extends <code>Mammal</code>, then a call to <code>get_class($this)</code> inside the <code>Dog</code> class will return 'Mammal.'.</p>
  
  <p>In other words:</p>
  
  <ul>
  <li>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."</li>
  <li>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."</li>
  <li>What I'm asking is, "can the Dog class itself tell me 'my name is Dog?'"</li>
  </ul>
  
  <p>For example:</p>
  
  <pre><code>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     
  }
  </code></pre>
  
  <h2>Update 2</h2>
  
  <p>Yacoby's suggestion of <code>get_called_class()</code> was correct. Here's how it works in the example I gave.</p>
  
  <pre><code>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     
      }
  </code></pre>