Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
<?php class Model { // Using self:: — WRONG for inheritance public static function createSelf(): static { return new self(); // Always creates Model, never the child } // Using static:: — CORRECT for inheritance (LSB) public static function create(): static { return new static(); // Creates the actual called class } // get_called_class() equivalent: static::class public static function getClass(): string { return static::class; // returns the runtime class name } } class User extends Model { public string $type = 'user'; } class Admin extends Model { public string $type = 'admin'; } $model = Model::create(); // instanceof Model $user = User::create(); // instanceof User ✓ $admin = Admin::create(); // instanceof Admin ✓ echo User::getClass(); // User echo Admin::getClass(); // Admin // Practical: active-record style finder class BaseRecord { private static array $store = []; public static function find(int $id): ?static { return static::$store[$id] ?? null; } public static function save(int $id, static $record): void { static::$store[$id] = $record; } } class UserRecord extends BaseRecord {} $u = new UserRecord(); UserRecord::save(1, $u); $found = UserRecord::find(1); // returns UserRecord instance
Result
Open