Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Prywatne wlasciwosci w singletonach
piczu
post
Post #1





Grupa: Zarejestrowani
Postów: 182
Pomógł: 0
Dołączył: 4.01.2005
Skąd: piczu.info

Ostrzeżenie: (10%)
X----


Czy wie ktos jak sprawdzic, czy singleton ma dana prywatna wlasciwosc ?
Prubowalem ReflectionClass, ale chyba przy wywolaniu new ReflectionClass('class') nie tworzy nowego obiektu z powodu prywatnego konstruktora i getDefaultProperties zwraca pusta tablice.

Oto cala moja funkcja:
  1. <?php
  2. function private_property_exists($class, $property) {
  3. if (is_object($class)) $class = get_class($class);
  4. $reflection = new ReflectionClass($class);
  5. return array_key_exists($property, $reflection->getDefaultProperties());
  6. }
  7. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dr_bonzo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Sorry. Zagalopowalem sie.

Patrz na to:
  1. <?php
  2. function private_property_exists($class, $property)
  3. {
  4. if ( is_object($class) )
  5. {
  6. $class = get_class($class);
  7. }
  8.  
  9. $reflection = new ReflectionClass($class);
  10.  
  11. // print_r( $reflection->getDefaultProperties() );
  12.  
  13. return array_key_exists($property, $reflection->getDefaultProperties());
  14. }
  15.  
  16. class SingletonClass
  17. {
  18. static private $instance = null;
  19.  
  20. public static function getInstance()
  21. {
  22. if ( is_null( self::$instance ) )
  23. {
  24. self::$instance = new SingletonClass();
  25. }
  26.  
  27. return self::$instance;
  28. }
  29.  
  30. private function __construct()
  31. {
  32. $this->x = 'xxx';
  33. $this->y = 'YYY';
  34. }
  35.  
  36. private $x;
  37. public $y;
  38. }
  39.  
  40.  
  41. $answ = private_property_exists( 'SingletonClass', 'x' );
  42. var_dump( $answ );
  43. ?>


i zwraca:

Kod
bool( true );

Czyli dziala (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 4.10.2025 - 14:39