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.
PHP - Read/Write public/protected/private object property
<?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'),"!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment