ReflectionProperty::setRawValue

(PHP 8 >= 8.4.0)

ReflectionProperty::setRawValueDefine la valor de una propiedad, omitiendo un hook de definición si está definido

Descripción

public ReflectionProperty::setRawValue(object $object, mixed $value): void

Define la valor de una propiedad, omitiendo un hook set si está definido.

Parámetros

object
El objeto sobre el cual definir la valor de la propiedad.
value
La valor a escribir. Debe ser siempre válida según el tipo de la propiedad.

Valores devueltos

No devuelve ningún valor.

Errores/Excepciones

Si la propiedad es virtual, se lanzará una Error, ya que no hay valor bruto a definir.

Ejemplos

Ejemplo #1 Ejemplo de ReflectionProperty::setRawValue()

<?php
class Example
{
public
int $age {
set {
if (
$value <= 0) {
throw new
\InvalidArgumentException();
}
$this->age = $value;
}
}
}

$example = new Example();

$rClass = new \ReflectionClass(Example::class);
$rProp = $rClass->getProperty('age');

// Esto pasaría por el hook set, y lanzaría una excepción.
$example->age = -2;
try {
$example->age = -2;
} catch (
InvalidArgumentException) {
print
"InvalidArgumentException para establecer la propiedad en -2\n";
}
try {
$rProp->setValue($example, -2);
} catch (
InvalidArgumentException) {
print
"InvalidArgumentException para usar ReflectionProperty::setValue() con -2\n";
}

// Pero esto establecería $age a -2 sin error.
$rProp->setRawValue($example, -2);
echo
$example->age;
?>

El resultado del ejemplo sería:

InvalidArgumentException para establecer la propiedad en -2
InvalidArgumentException para usar ReflectionProperty::setValue() con -2
-2
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top