<?php
// This is a basic class without any special behavior. No deprecation error occur despite the public_property being unset.
$foo = new Foo();
$foo->public_property = true;
unset( $foo->public_property );
$foo->public_property = true;
// A class with a __set magic method that allows modifying declared public properties, mimicking the basic class behaviour and providing more backward compatibility.
$baz = new Baz();
$baz->public_property = true;
unset( $baz->public_property );
$baz->public_property = true;
// This is a class with a __set magic method that restricts dynamic property creation.
$bar = new Bar();
$bar->public_property = true;
unset( $bar->public_property );
$bar->public_property = true;
class Foo {
public $public_property;
}
class Baz {
public $public_property;
public function __set( $prop, $value ) {
if ( 'known_dynamic_property' === $prop ) {
// Dynamic property-specific logic goes here.
return;
}
// Checks if the property is a known public property and allows setting it.
if ( 'public_property' === $prop ) {
$this->public_property = $value;
return;
}
trigger_error(
sprintf( 'Setting the dynamic property "%s" on %s is deprecated', $prop, __CLASS__ ),
E_USER_DEPRECATED
);
}
}
class Bar {
public $public_property;
public function __set( $prop, $value ) {
if ( 'known_dynamic_property' === $prop ) {
// Dynamic property-specific logic goes here.
return;
}
trigger_error(
sprintf( 'Setting the dynamic property "%s" on %s is deprecated', $prop, __CLASS__ ),
E_USER_DEPRECATED
);
}
}