yep/reflection

PHP class reflection enhancements

v2.1.0 2017-10-27 22:11 UTC

This package is auto-updated.

Last update: 2024-11-11 18:44:49 UTC


README

Build Status Scrutinizer Code Quality Scrutinizer Code Coverage Latest Stable Version Total Downloads License

PHP class reflection enhancements (For PHP7+)

Packagist

Reflection is available on Packagist.org, just add the dependency to your composer.json.

{
  "require" : {
    "yep/reflection": "^2.0"
  }
}

or run Composer command:

php composer.phar require yep/reflection

What do Yep/Reflection?

"Test subject" and example code

<?php
class SomeClass {
	private $someProperty;

	protected function someMethod($someArgument) {
		return $someArgument;
	}

	public function getSomeProperty() {
		return $this->someProperty;
	}

	public function setSomeProperty($someProperty) {
		$this->someProperty = $someProperty;
	}
}

class SomeClass2 extends SomeClass {
}

$someClass = new SomeClass();
$reflection = \Yep\Reflection\ReflectionClass::from($class = $someClass);

You can simply call the protected or private method

<?php
$someClass = new SomeClass();

echo $reflection->invokeMethod($method = 'someMethod', $arguments = ['foo']); // 'foo'

You can simply set value to the protected or private property

<?php
$someClass = new SomeClass();

$reflection->setPropertyValue($property = 'someProperty', $value = 'foo');

echo $someClass->getSomeProperty(); // 'foo'

You can simply get value from the protected or private property

<?php
$someClass = new SomeClass();
$someClass->setSomeProperty('foo');

echo $reflection->getPropertyValue($property = 'someProperty'); // 'foo';

Wanna access parent or parent's private property?

<?php
$someClass = new SomeClass2();
$someClass->setSomeProperty('foo');

echo $reflection->getParent()->getPropertyValue($property = 'someProperty'); // 'foo';