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());
Output for 7.0.0 - 7.0.23, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
string(35) "new Vehicle('vehicle') is a Vehicle" string(35) "new Bicycle('bicycle') is a Bicycle" string(23) "new Car('car') is a Car" string(30) "$car->getSelfType() is vehicle" string(28) "$car->getStaticType() is car" string(50) "Vehicle::createStatic('staticThing1') is a Vehicle" string(50) "Bicycle::createStatic('staticThing2') is a Bicycle" string(42) "Car::createStatic('staticThing3') is a Car" string(39) "$staticThing3->getSelfType() is vehicle" string(37) "$staticThing3->getStaticType() is car" string(46) "Vehicle::createSelf('selfThing1') is a Vehicle" string(46) "Bicycle::createSelf('selfThing2') is a Vehicle" string(42) "Car::createSelf('selfThing3') is a Vehicle" string(37) "$selfThing3->getSelfType() is vehicle" string(39) "$selfThing3->getStaticType() is vehicle"

preferences:
117.73 ms | 410 KiB | 5 Q