too much php
too much php

Reputation: 91088

Shorter way to access name of class in static method?

Is there a better/shorter way to write the whoAmI method in the following code? It seems a bit unnecessary to create a new object just to get the static class' name.

<?php

abstract class baseClass {
    static function whoAmI() {
        echo get_class(new static); // Making a new class just to get its name???
    }
}

Upvotes: 3

Views: 317

Answers (1)

klakegg
klakegg

Reputation: 181

Try get_called_class().

http://php.net/manual/en/function.get-called-class.php

Upvotes: 9

Related Questions