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 git.master, git.master_jit, rfc.property-hooks
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"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
26.94 ms | 408 KiB | 5 Q