0

buildTableName() should convert the class name from "FooRepository" to "foo" but it returns an empty string although the class name is correct when printed in function query(). What's wrong in here?

abstract class Repository
{

    protected function buildTableName(): string
    {
        $shortName = (new \ReflectionClass(self::class))->getShortName();
        $tableName = str_replace('Repository', '', $shortName);
        return strtolower($tableName);
    }

}

class FooRepository extends Repository
{
    public function query()
    {   
        echo "my class is ".self::class;
        echo $this->buildTableName();
    }
}


(new FooRepository())->query();
MadCatERZ
  • 113
  • 7
  • 1
    https://stackoverflow.com/questions/11710099/what-is-the-difference-between-selfbar-and-staticbar-in-php – aynber Jun 15 '23 at 19:43
  • 2
    Because self::class in buildTableName always refers to Repository class. Change self to static keyword – LordF Jun 15 '23 at 19:43

0 Answers0