Skip to content

Instantly share code, notes, and snippets.

@TheDistantSea
Forked from lastguest/access_property.php
Created December 14, 2015 16:14
Show Gist options
  • Select an option

  • Save TheDistantSea/7e88404bfa6a5fb3ff47 to your computer and use it in GitHub Desktop.

Select an option

Save TheDistantSea/7e88404bfa6a5fb3ff47 to your computer and use it in GitHub Desktop.

Revisions

  1. @lastguest lastguest revised this gist Jul 10, 2013. 1 changed file with 37 additions and 1 deletion.
    38 changes: 37 additions & 1 deletion access_property.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    <?php

    // Access a property with no restrictions
    function stole($object,$property){
    $dict = (array)$object;
    $class = get_class($object);
    @@ -9,10 +10,45 @@ function stole($object,$property){
    $dict["\0$class\0$property"]:null));
    }


    // Modify a property with no restrictions
    function inject(&$object,$property,$value){
    $dict = (array)$object;
    $class = get_class($object);
    if (isset($dict["\0$class\0$property"])) {
    $dict["\0$class\0$property"] = $value;
    $object = castToClass($dict,$class);
    } elseif (isset($dict["\0*\0$property"])) {
    $dict["\0*\0$property"] = $value;
    $object = castToClass($dict,$class);
    } elseif (isset($dict[$property])) {
    $object->$property = $value;
    } else {
    return ;
    }
    }

    // Required because we don't know if className has a __set_state defined.
    function castToClass(array $array,$className) {
    return unserialize(
    'O:'.strlen($className).':"'
    .$className.'"'.strstr(serialize($array), ':')
    );
    }


    // Example

    class Safe {
    private $content = 'gold';
    }

    $mySafe = new Safe();

    echo "I've robbed your ",stole($mySafe,'content'),"!";
    // Inject value into a private property
    inject($mySafe,'content','Magic!');


    echo "I've robbed your ",stole($mySafe,'content'),"!";


  2. @lastguest lastguest revised this gist Jul 10, 2013. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions access_property.php
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,10 @@
    function stole($object,$property){
    $dict = (array)$object;
    $class = get_class($object);
    $Z = "\0";
    return isset($dict[$property])?
    $dict[$property]:(isset($dict["$Z*$Z$property"])?
    $dict["$Z*$Z$property"]:(isset($dict["$Z$class$Z$property"])?
    $dict["$Z$class$Z$property"]:null));
    $dict[$property]:(isset($dict["\0*\0$property"])?
    $dict["\0*\0$property"]:(isset($dict["\0$class\0$property"])?
    $dict["\0$class\0$property"]:null));
    }

    class Safe {
  3. @lastguest lastguest created this gist Jul 10, 2013.
    19 changes: 19 additions & 0 deletions access_property.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?php

    function stole($object,$property){
    $dict = (array)$object;
    $class = get_class($object);
    $Z = "\0";
    return isset($dict[$property])?
    $dict[$property]:(isset($dict["$Z*$Z$property"])?
    $dict["$Z*$Z$property"]:(isset($dict["$Z$class$Z$property"])?
    $dict["$Z$class$Z$property"]:null));
    }

    class Safe {
    private $content = 'gold';
    }

    $mySafe = new Safe();

    echo "I've robbed your ",stole($mySafe,'content'),"!";