@ 2024-03-28T18:32:50Z <?php
/**
* Example of dynamic property that is initialized only when the property is invoked,
* i.e. only on get.
*/
class DynamicPropInitOnGet {
public $prop1;
public $prop2;
public $prop3;
public function __construct( $props ) {
foreach ( $props as $prop => $value ) {
$this->$prop = $value;
}
}
public function __get( $key ) {
switch ( $key ) {
case 'known':
$known = new stdClass();
foreach ( array( 'prop1', 'prop2' ) as $column ) {
$known->{$column} = isset( $this->{$column} ) ? $this->{$column} : null;
}
return $known;
}
}
}
class TestMagicMethods {
private $obj;
private $is_known_prop = false;
public function __construct( $obj ) {
$this->obj = $obj;
}
public function test( $prop ) {
$this->is_known_prop = ( 'known' === $prop );
// __isset.
$this->test_isset( $prop );
// __get().
$this->test_get( $prop );
// __isset() after __get().
$this->test_isset( $prop, true );
// __unset().
$this->test_unset( $prop );
}
private function test_isset( $prop, $after_get = false ) {
$actual = isset( $this->obj->$prop );
if ( ! $after_get ) {
printf( "__isset() results should be false %s\n", $this->test_results( false === $actual ) );
} else {
$expected = $this->is_known_prop;
printf(
"\n\n__isset() after __get() results should be %s %s\n",
$this->is_known_prop ? 'true' : 'false',
$this->test_results( $expected == $actual )
);
}
var_dump( $actual );
}
private function test_get( $prop ) {
$actual = $this->obj->$prop;
$message = "\n\n__get() results should be %s %s\n";
if ( $this->is_known_prop ) {
printf( $message, 'an object with prop1 and prop2', $this->test_results( $actual instanceof stdClass ) );
} else {
printf( $message, 'null', $this->test_results( null === $actual ) );
}
var_dump( $actual );
}
private function test_set( $prop ) {
$value = $this->is_known_prop ? (array) $this->$obj->known : 'I am an unknown dynamic property';
$obj->$prop = $value;
$actual = $this->obj->$prop;
if ( $this->is_known_prop ) {
printf(
"\n\n" .
'__set() results should set the value %s and not throw a deprecation on PHP 8.2+.' .
'However, once the __get() is called again, it will set reset it to an object with prop1 and prop2.' .
"\n",
$this->test_results( $actual instanceof stdClass )
);
} else {
printf(
"\n\n" .
'__set() results should set the value %s and not throw a deprecation on PHP 8.2+.' .
'The set value should remain set when invoking __get()' .
"\n",
$this->test_results( $value === $actual )
);
}
var_dump( $actual );
}
private function test_unset( $prop ) {// unset.
unset( $this->obj->$prop );
$actual = $this->obj->$prop;
$message = "\n\n__unset() + __get() results should be %s %s\n";
if ( $this->is_known_prop ) {
printf(
$message,
're-init the object with prop1 and prop2',
$this->test_results( $actual instanceof stdClass )
);
} else {
printf(
$message,
'null',
$this->test_results( null === $actual )
);
}
var_dump( $actual );
}
private function test_results( $actual ) {
return $actual ? '✅' : '❌';
}
}
$obj = new DynamicPropInitOnGet(
array(
'prop1' => 'foo',
'prop2' => 'bar',
'prop3' => 'baz',
)
);
$tester = new TestMagicMethods( $obj );
echo "***** Test the 'known' property *****\n\n";
$tester->test( 'known' );
echo "\n\n***** Unknown, unexpected dynamic property *****\n\n";
$tester->test( 'unknown' );
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.4.1 0.008 0.000 19.48 8.3.14 0.011 0.004 20.71 8.3.13 0.006 0.003 18.24 8.3.12 0.000 0.010 19.10 8.3.11 0.008 0.000 20.94 8.3.10 0.009 0.000 24.06 8.3.9 0.004 0.011 26.77 8.3.8 0.004 0.004 16.63 8.3.7 0.010 0.007 18.30 8.3.6 0.011 0.007 16.67 8.3.5 0.018 0.004 16.81 8.3.4 0.003 0.020 19.84 8.3.3 0.010 0.013 19.05 8.3.2 0.011 0.011 21.73 8.3.1 0.019 0.003 21.90 8.3.0 0.007 0.015 18.99 8.2.25 0.015 0.003 18.45 8.2.24 0.000 0.009 17.46 8.2.23 0.008 0.000 22.58 8.2.22 0.004 0.008 37.54 8.2.21 0.006 0.003 26.77 8.2.20 0.006 0.003 16.75 8.2.19 0.013 0.010 16.75 8.2.18 0.012 0.009 16.63 8.2.17 0.010 0.013 18.94 8.2.16 0.000 0.016 19.97 8.2.15 0.007 0.014 19.06 8.2.14 0.012 0.009 20.56 8.2.13 0.011 0.011 20.39 8.2.12 0.007 0.011 19.27 8.2.11 0.009 0.006 19.31 8.2.10 0.018 0.004 19.14 8.2.9 0.015 0.006 18.96 8.2.8 0.010 0.010 19.44 8.2.7 0.013 0.006 18.63 8.2.6 0.008 0.008 18.85 8.2.5 0.012 0.003 19.31 8.2.4 0.004 0.012 18.82 8.2.3 0.007 0.013 18.84 8.2.2 0.013 0.006 18.78 8.2.1 0.013 0.003 18.70 8.2.0 0.007 0.011 18.83 8.1.30 0.007 0.011 19.54 8.1.29 0.003 0.006 18.88 8.1.28 0.007 0.014 25.92 8.1.27 0.009 0.006 22.07 8.1.26 0.006 0.013 21.84 8.1.25 0.015 0.004 22.13 8.1.24 0.006 0.010 18.48 8.1.23 0.010 0.010 18.62 8.1.22 0.012 0.009 19.07 8.1.21 0.012 0.004 18.57 8.1.20 0.009 0.006 18.66 8.1.19 0.017 0.000 18.65 8.1.18 0.004 0.015 18.38 8.1.17 0.014 0.004 18.82 8.1.16 0.010 0.006 18.35 8.1.15 0.004 0.011 18.87 8.1.14 0.000 0.015 18.76 8.1.13 0.009 0.006 18.86 8.1.12 0.006 0.009 18.52 8.1.11 0.012 0.003 18.58 8.1.10 0.009 0.006 19.74 8.1.9 0.011 0.004 18.68 8.1.8 0.009 0.006 19.73 8.1.7 0.012 0.003 18.80 8.1.6 0.011 0.004 18.75 8.1.5 0.015 0.000 18.63 8.1.4 0.011 0.004 18.81 8.1.3 0.007 0.007 19.04 8.1.2 0.009 0.006 20.11 8.1.1 0.006 0.009 18.60 8.1.0 0.000 0.015 18.43
preferences:dark mode live preview
25.58 ms | 403 KiB | 5 Q