Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorter way to access name of class in static method?

Tags:

php

class

php-5.3

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???
    }
}
like image 210
too much php Avatar asked Dec 09 '25 15:12

too much php


1 Answers

Try get_called_class().

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

like image 112
klakegg Avatar answered Dec 12 '25 05:12

klakegg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!