3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Vehicle { /** * @var string */ private $name; protected static $type = 'vehicle'; /** * Vehicle constructor. * * @param $name */ public function __construct($name) { $this->name = $name; } /** * @param $type * @return static */ public static function createStatic($type) { return new static($type); } /** * @param $type * @return \Vehicle */ public static function createSelf($type) { return new self($type); } /** * @return string */ public function getStaticType() { return static::$type; } /** * @return string */ public function getSelfType() { return self::$type; } } class Bicycle extends Vehicle { protected static $type = 'bicycle'; /**getSelfType * @inheritDoc */ public function __construct($name) { parent::__construct($name); } } class Car extends Vehicle { protected static $type = 'car'; /** * @inheritDoc */ public function __construct($name) { parent::__construct($name); } } $vehicle = new Vehicle('vehicle1'); $bicycle = new Bicycle('bicycle1'); $car = new Car('car1'); var_dump('new Vehicle(\'vehicle\') is a ' . get_class($vehicle)); var_dump('new Bicycle(\'bicycle\') is a ' . get_class($bicycle)); var_dump('new Car(\'car\') is a ' . get_class($car)); var_dump('$car->getSelfType() is ' . $car->getSelfType()); var_dump('$car->getStaticType() is ' . $car->getStaticType()); $staticThing1 = Vehicle::createStatic('staticThing1'); $staticThing2 = Bicycle::createStatic('staticThing2'); $staticThing3 = Car::createStatic('staticThing3'); var_dump('Vehicle::createStatic(\'staticThing1\') is a ' . get_class($staticThing1)); var_dump('Bicycle::createStatic(\'staticThing2\') is a ' . get_class($staticThing2)); var_dump('Car::createStatic(\'staticThing3\') is a ' . get_class($staticThing3)); var_dump('$staticThing3->getSelfType() is ' . $staticThing3->getSelfType()); var_dump('$staticThing3->getStaticType() is ' . $staticThing3->getStaticType()); $selfThing1 = Vehicle::createSelf('selfThing1'); $selfThing2 = Bicycle::createSelf('selfThing2'); $selfThing3 = Car::createSelf('selfThing3'); var_dump('Vehicle::createSelf(\'selfThing1\') is a ' . get_class($selfThing1)); var_dump('Bicycle::createSelf(\'selfThing2\') is a ' . get_class($selfThing2)); var_dump('Car::createSelf(\'selfThing3\') is a ' . get_class($selfThing3)); var_dump('$selfThing3->getSelfType() is ' . $selfThing3->getSelfType()); var_dump('$selfThing3->getStaticType() is ' . $selfThing3->getStaticType());

preferences:
29.56 ms | 404 KiB | 5 Q