Skip to content

Instantly share code, notes, and snippets.

@afoeder
Created January 13, 2012 09:30
Show Gist options
  • Select an option

  • Save afoeder/1605272 to your computer and use it in GitHub Desktop.

Select an option

Save afoeder/1605272 to your computer and use it in GitHub Desktop.

Revisions

  1. afoeder revised this gist Jan 13, 2012. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions ReflectionDemo.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,12 @@
    <?php
    /**
    * Please be aware that private properties are private for good purpose,
    * changing it during runtime so is only OK during Unit Testing for example.
    * So use this only if you're knowing what you're doing :)
    */



    class BareDemoClass() {
    protected $interestingThing = 42;

  2. afoeder revised this gist Jan 13, 2012. 1 changed file with 18 additions and 2 deletions.
    20 changes: 18 additions & 2 deletions ReflectionDemo.php
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,30 @@
    <?php
    class BareDemoClass() {
    protected $interestingThing = 42;

    protected function niceButPrivateMethod() {
    return "DNA N47°16' E11°23'";
    }
    }

    // during usual use, you'd have both the bare class and $instanciatedClass available
    $instanciatedClass = new BareDemoClass();


    $reflectedProperty = new \ReflectionProperty('BareDemoClass', 'interestingThing');
    $reflectedProperty->setAccessible(true);
    $reflectedProperty->setAccessible(TRUE);
    // 42
    echo $reflectedProperty->getValue($instanciatedClass);

    // the idea is to have the property at first as "anonymous" property description without context; and with ->getValue you give the context: you pass in the desired object.
    $reflectedMethod = new \ReflectionMethod('BareDemoClass', 'niceButPrivateMethod');
    $reflectedMethod->setAccessible(TRUE);
    // DNA N47°16' E11°23'
    echo $reflectedMethod->invoke($instanciatedClass);


    // you could also get the reflected property or method using the object itself:
    $reflectedObject = new \ReflectionObject($instanciatedClass);
    $reflectedProperty = $reflectedObject->getProperty('interestingThing');
    // continue like above; or
    $reflectedMethod = $reflectedObject->getMethod('niceButPrivateMethod');
    ?>
  3. afoeder created this gist Jan 13, 2012.
    14 changes: 14 additions & 0 deletions ReflectionDemo.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php
    class BareDemoClass() {
    protected $interestingThing = 42;
    }

    // during usual use, you'd have both the bare class and $instanciatedClass available
    $instanciatedClass = new BareDemoClass();

    $reflectedProperty = new \ReflectionProperty('BareDemoClass', 'interestingThing');
    $reflectedProperty->setAccessible(true);
    echo $reflectedProperty->getValue($instanciatedClass);

    // the idea is to have the property at first as "anonymous" property description without context; and with ->getValue you give the context: you pass in the desired object.
    ?>