Skip to content

Instantly share code, notes, and snippets.

@ivastly
Created February 14, 2021 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivastly/152fa00f24916c137137275b24c39218 to your computer and use it in GitHub Desktop.
Save ivastly/152fa00f24916c137137275b24c39218 to your computer and use it in GitHub Desktop.
Usage of php-reflection library - reading private property of an object in PHP.
<?php
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
class ParentClass
{
private $property = 'parent private property';
}
class C extends ParentClass
{
}
$object = new C();
$reflection = new \Ivastly\PhpReflection\Reflection();
$value = $reflection->getProperty($object, 'property');
$visibility = $reflection->getVisibility($object, 'property');
echo "$visibility \$property = '$value;'\n"; // private $property = 'parent private property';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment