Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting class name from static function in abstract class

I have an abstract class that has a number of static functions (which return a new instance of itself by using new static($args) which works fine), but I can't work out how to get the class name. I am trying to avoid putting

protected static $cn = __CLASS__;

but if unavoidable, then its not the end of the world

abstract class ExtendableObject {
    static function getObject() {
        return new static($data);
    }

    static function getSearcher() {
        return new ExtendableObjectFinder(/* CLASS NAME CLASS */);
    }
}

class ExtendableObjectFinder {
    private $cn;

    function __construct($className) {
       $this->cn = $className;
    }

    function where($where) { ... }

    function fetch() { ... }
}
like image 546
topherg Avatar asked Nov 16 '25 11:11

topherg


1 Answers

To get the name of the class you can use get_class and pass $this.

Alternatively, there is get_called_class which you can use within static methods.

like image 98
Brad Christie Avatar answered Nov 18 '25 05:11

Brad Christie



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!