NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Senior0 views

PHP task: what will the `$user` and `$product` variables contain when the static method `getTableName()` is called in the `User` and `Product` subclasses? Explain your reasoning.

Practice

Asked at

AI answer

The exact result depends on the method's code and classes, which are not provided in the question. An important PHP rule is late static binding. In an inherited static method, static:: refers to the class through which the method was called, while self:: refers to the class where the method was declared.

class Model
{
    public static function getTableName(): string
    {
        return static::$table;
    }
}

class User extends Model { protected static string $table = 'users'; }
class Product extends Model { protected static string $table = 'products'; }

$user = User::getTableName();       // 'users'
$product = Product::getTableName(); // 'products'

If the method returns static::class, the values will be 'User' and 'Product'; with self::class in the Model method, both calls will return 'Model'. Therefore, to give an exact answer, we need to know the implementation of getTableName() and the property declarations.