ReflectionProperty::__construct

creationdev

Documentation

Conception web standard
Conception web standard

2500$ CAD

Référencement web organique
Référencement web organique

300$ CAD

Formation référencement web
Formation référencement web

100$ CAD

Conception web ecommerce
Conception web ecommerce

2500$ CAD

Conception web extra
Conception web extra

5000$ CAD

Conception d'application web
Conception d'application web

100$ CAD

Référencement web expert
Référencement web expert

750$ CAD

Référencement web optimisé
Référencement web optimisé

500$ CAD

Référencement web stratégique
Référencement web stratégique

1000$ CAD

Conception web microsite
Conception web microsite

1000$ CAD

Méthode

Méthode

ReflectionProperty::__construct

ReflectionProperty::__construct

Page Vue 26 déc. 2018
Page Vue 14 fois

(PHP 5, PHP 7, PHP 8)

ReflectionProperty::__construct — Construit un nouvel objet ReflectionProperty

Description

public ReflectionProperty::__construct(object|string $class, string $property)

Liste de paramètres

class

Soit une chaîne de caractère contenant le nom de la classe à introspecter, ou un objet.

property

Le nom de la propriété à refléter.

Erreurs / Exceptions

Le fait de tenter de récupérer ou définir la valeur d'une propriété privée ou protégée d'une classe émettra une exception.

Exemples

Exemple #1 Exemple avec ReflectionProperty::__construct()

<?php
class Str
{
public $length = 5;
}

// Création d'une instance de la classe ReflectionProperty
$prop = new ReflectionProperty('Str', 'length');

// Affichage de quelques informations
printf(
"===> The%s%s%s%s property '%s' (which was %s)\n" .
" having the modifiers %s\n",
$prop->isPublic() ? ' public' : '',
$prop->isPrivate() ? ' private' : '',
$prop->isProtected() ? ' protected' : '',
$prop->isStatic() ? ' static' : '',
$prop->getName(),
$prop->isDefault() ? 'declared at compile-time' : 'created at run-time',
var_export(Reflection::getModifierNames($prop->getModifiers()), 1)
);

// Création d'une instance de Str
$obj= new Str();

// Récupère la valeur courante
printf("---> Value is: ");
var_dump($prop->getValue($obj));

// Modifie la valeur
$prop->setValue($obj, 10);
printf("---> Setting value to 10, new value is: ");
var_dump($prop->getValue($obj));

// Affiche l'objet
var_dump($obj);
?>

Résultat de l'exemple ci-dessus est similaire à :

===> The public property 'length' (which was declared at compile-time)
     having the modifiers array (
  0 => 'public',
)
---> Value is: int(5)
---> Setting value to 10, new value is: int(10)
object(Str)#2 (1) {
  ["length"]=>
  int(10)
}

Exemple #2 Récupération d'une valeur depuis une propriété privée ou protégée en utilisant la classe ReflectionProperty

<?php

class Foo {
public $x = 1;
protected $y = 2;
private $z = 3;
}

$obj = new Foo;

$prop = new ReflectionProperty('Foo', 'y');
$prop->setAccessible(true);
var_dump($prop->getValue($obj)); // int(2)

$prop = new ReflectionProperty('Foo', 'z');
$prop->setAccessible(true);
var_dump($prop->getValue($obj)); // int(2)

?>

Résultat de l'exemple ci-dessus est similaire à :

int(2)
int(3)

Voir aussi

  • ReflectionProperty::getName() - Récupère le nom de la propriété
  • Les constructeurs


Source : http://www.php.net/
Ref : methode-reflectionproperty::--construct.php




Contact

Contact

Veuillez saisir les infos dans le formulaire d'identification
suivant pour concevoir et référencer votre site.









Veuillez saisir votre message.

Carte d'affaires

Affichage de la carte d'affaires recto/verso.

Carte d'affaires recto
Carte d'affaires verso

Partagez

Partagez

Discussion ayant comme sujet la page ReflectionProperty::__construct.

©2025 creationdev -  F.A.Q. -  Soutien -  Haut de page
THÈMES: