3v4l.org

run code in 300+ PHP versions simultaneously
<?php function cast($source, $class) { try { $sourceReflection = new ReflectionClass($source); $targetReflection = new ReflectionClass($class); $targetProperties = $targetReflection->getProperties(); $target = $targetReflection->newInstanceWithoutConstructor(); foreach ($targetProperties as $targetProperty) { $name = $targetProperty->getName(); if ($sourceReflection->hasProperty($name)) { $sourceProperty = $sourceReflection->getProperty($name); $sourceProperty->setAccessible(true); $targetProperty->setAccessible(true); $targetProperty->setValue($target, $sourceProperty->getValue($source)); } } return $target; } catch (ReflectionException $reflectionException) { return null; } } class Foo { private $a = __CLASS__; private $b = __CLASS__; private $c = __CLASS__; } class Bar { private $b = __CLASS__; private $c = __CLASS__; private $d = __CLASS__; } $foo = new Foo(); $bar = cast($foo, 'Bar'); var_dump($bar);
Output for git.master, git.master_jit, rfc.property-hooks
object(Bar)#7 (3) { ["b":"Bar":private]=> string(3) "Foo" ["c":"Bar":private]=> string(3) "Foo" ["d":"Bar":private]=> string(3) "Bar" }

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:
35.23 ms | 401 KiB | 8 Q