PHP 8.3.4 Released!

ReflectionProperty::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionProperty::__constructBir ReflectionProperty nesnesi oluşturur

Açıklama

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

Bir ReflectionProperty örneği oluşturur.

Bağımsız Değişkenler

class

Yansıtılacak sınıfın ismini içeren bir dizge veya bir nesne.

property

Yansıtılacak özelliğin ismi.

Hatalar/İstisnalar

Private veya protected sınıf özelliklerinin değerlerini değiştirme veya döndürme girişimleri bir istisna oluşmasına sebep olur.

Örnekler

Örnek 1 - ReflectionProperty::__construct() örneği

<?php
class Dizge
{
public
$uzunluk = 5;
}

// ReflectionProperty sınıfının bir örneğini oluşturalım
$prop = new ReflectionProperty('Dizge', 'uzunluk');

// Print out basic information
printf(
"===> %s%s%s%s '%s' özelliği %s olup\n" .
" %s değiştiriciye sahiptir.\n",
$prop->isPublic() ? ' public' : '',
$prop->isPrivate() ? ' private' : '',
$prop->isProtected() ? ' protected' : '',
$prop->isStatic() ? ' static' : '',
$prop->getName(),
$prop->isDefault() ? 'derleme sırasında bildirilmiş' :
'çalışma anında oluşturulmuş',
var_export(Reflection::getModifierNames($prop->getModifiers()), 1)
);

// Dizge sınıfının bir örneğini oluşturalım
$nesne= new Dizge();

// Mevcut değeri öğrenelim
printf("---> Değeri: ");
var_dump($prop->getValue($nesne));

// Değeri değiştirelim
$prop->setValue($nesne, 10);
printf("---> Yeni değer olarak 10 atandıktan sonra yeni değer: ");
var_dump($prop->getValue($nesne));

// Nesneyi dökümü
var_dump($nesne);
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

===>  public 'uzunluk' özelliği derleme sırasında bildirilmiş olup
    array (
  0 => 'public',
) değiştiriciye sahiptir.
---> Değeri: int(5)
---> Yeni değer olarak 10 atandıktan sonra yeni değer: int(10)
object(Dizge)#2 (1) {
  ["uzunluk"]=>
  int(10)
}

Örnek 2 - ReflectionProperty sınıfından private ve protected özelliklerin değerlerini öğrenmek

<?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)

?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

int(2)
int(3)

Ayrıca Bakınız

add a note

User Contributed Notes 1 note

up
5
geoffsmiths at hotmail dot com
6 years ago
At example #2: the comment // int(2) is stated while the value for the private property is actually 3. (private $z = 3;)

var_dump($prop->getValue($obj)); // This should be int(3)
To Top