Core\Reflect\ClassInfo::set
$classInfo->set(mixed $object, string $name, mixed $value): void$classInfo->set( mixed $object, string $name, mixed $value, ): void
Writes a property of an object when the name of the property is in a string.
set($object, $name, $value) writes $value into the property $name of $object. The object must be an instance of the described class. set follows the same rules as a normal write at the same place in your code. A private property cannot be written from outside its class, and there is no way to switch this check off. The value must have the type the property declares. If the class watches its properties, set informs it the same way a normal write does.
A private property and a value of the wrong type throw a RuntimeError. A name that is not a property of the class throws a LogicError. After an error, the object is unchanged.
The examples below write two properties by name, show the three errors, and fill a settings object from configuration values.
Parameters
$objectmixedAn instance of the described class. You pass it here for the same reason as inget.$namestringThe property's name without the$, aspropertiesreturns it. The text cannot betainted. Check text from a request withCore\Taint::assertTrustedfirst.$valuemixedThe value to store. It is checked against the type that the object's class declares for the property, because that type is not known at compile time. Ataintedorsecretvalue, or amixedvalue, does not compile here.
Return value
Returns void. Nothing. A following get returns the stored value, and the observer is told about that value.
Errors
RuntimeError$objectis not an object,$nameis a property that is notpublic, or$valuedoes not have the type that the property declares. Ordinary code outside the class gets the same errors for the first two. A write through amixedvalue gets the same error for the third.LogicError$objectis not an instance of the described class, or the class has no property called$name. As inget, these are mistakes in the program, so they have a different error class.ThrowableWhatever the class's ownonPropertySetobserver throws. The observer runs after the value is stored. If it throws, the write still fails, as an ordinary write does.
Examples
Write a property by name
<?nvs// Shows how to write a property when its name is in a string.
class Profile{ public string $city = 'Berlin'; public int $age = 30;}
Profile $profile = new Profile();var $info = Core\Reflect::forObject($profile);
// `set` takes the object, the property name and the new value.$info->set($profile, 'city', 'Lisbon');$info->set($profile, 'age', 31);
// This prints `Lisbon 31`.echo $profile->city, ' ', $profile->age, "\n";Lisbon 31The errors set can throw
<?nvs// Shows the three errors `set` throws, and that the object does not change after an error.
class Session{ public int $visits = 0; private string $token = 'abc123';}
class Write{ public static function try(Session $session, string $name, int|string $value): string { var $info = Core\Reflect::forObject($session); try { $info->set($session, $name, $value); return $name . ': written'; } catch (RuntimeError $refused) { // A private property, or a value of the wrong type. return $name . ': RuntimeError'; } catch (LogicError $mistake) { // A name that is not a property of the class. return $name . ': LogicError'; } }}
Session $session = new Session();// The first line is written. The other three throw an error.echo Write::try($session, 'visits', 5), "\n";echo Write::try($session, 'token', 'stolen'), "\n";echo Write::try($session, 'visits', 'many'), "\n";echo Write::try($session, 'vists', 6), "\n";
// This prints `5`. No failed write changed the object.echo $session->visits, "\n";visits: writtentoken: RuntimeErrorvisits: RuntimeErrorvists: LogicError5Fill a settings object from configuration
<?nvs// Shows how a program fills a settings object from key and value pairs read from a configuration file.
class MailSettings{ public string $host = 'localhost'; public int $port = 25; public bool $secure = false;}
class Settings{ public static function fill(mixed $settings, array<string|int|bool> $values): tainted string { var $info = Core\Reflect::forObject($settings); tainted string $skipped = ''; foreach ($values as tainted string $key => string|int|bool $value) { // A key that is not a property is reported and not written. if (!$info->hasProperty($key)) { $skipped = $skipped . ' ' . $key; continue; } // `set` needs a plain string name. The check above found the name on the class. string $name = Core\Taint::assertTrusted($key, 'hasProperty found this name on the class'); $info->set($settings, $name, $value); } return 'skipped:' . $skipped; }}
array<string|int|bool> $configured = [ 'host' => 'mail.example.com', 'port' => 587, 'secure' => true, 'timeout' => 30,];
MailSettings $mail = new MailSettings();// This prints `skipped: timeout`, then the three values from the configuration.echo Settings::fill($mail, $configured), "\n";echo $mail->host, ':', $mail->port, ' secure=', $mail->secure ? 'yes' : 'no', "\n";skipped: timeoutmail.example.com:587 secure=yesAn object of the wrong class is refused
<?nvs// Shows that `set` checks the class of an object before it writes it into a property.
class Node{ public string $label = 'first'; public ?Node $next = null;}
class Other{ public string $label = 'other';}
Node $node = new Node();var $info = Core\Reflect::forObject($node);
// `$next` has the type `?Node`. An `Other` object is not a `Node`,// so `set` throws a `RuntimeError`.try { $info->set($node, 'next', new Other()); echo "written\n";} catch (RuntimeError $refused) { echo "refused\n";}
// A `Node` object has the right class, so this write works.$info->set($node, 'next', new Node());echo "written\n";refusedwritten