Skip to content

Repository files navigation

Igni logo

Build StatusScrutinizer Code QualityCode Coverage

Installation

composer require igniphp/reflection-api

Reflection API

Licensed under MIT License.

Reflection api provides tools that allows to:

  • read and write object's properties
  • build classes on runtime
  • retrieves closure's body
  • instantiating objects without reflection api

Reading object's properties

useIgni\Utils\ReflectionApi;
class TestClass
{
private$test;
publicfunction__construct()
{
$this->test = 1;
}
}
$instance = newTestClass();
ReflectionApi::readProperty($instance, 'test');

Write object's properties

useIgni\Utils\ReflectionApi;
class TestClass
{
private$test;
publicfunction__construct()
{
$this->test = 1;
}
}
$instance = newTestClass();
ReflectionApi::writeProperty($instance, 'test', 2);

Create an instance

useIgni\Utils\ReflectionApi;
class TestClass
{
private$test;
publicfunction__construct()
{
$this->test = 1;
}
}
$instance = ReflectionApi::createInstance(TestClass::class);

Building and loading class on runtime

useIgni\Utils\ReflectionApi;
useIgni\Utils\ReflectionApi\RuntimeProperty;
useIgni\Utils\ReflectionApi\RuntimeMethod;
$class = ReflectionApi::createClass('TestClass');
$class->addProperty((new RuntimeProperty('test))->makePrivate());$constructor = new RuntimeMethod('__construct');
$constructor->addBody(
'$this->test = 1'
);
$getTest = newRuntimeMethod('getTest');
$getTest->setReturnType('string');
$getTest->addBody(
'return $this->test'
);
$class->addMethod($constructor);
$class->addMethod($getTest);
$class->load();
$instance = $class->createInstance();
$instanceinstanceof'TestClass';// true.$instance->getTest();// 1

About

Reflection api and class builder kit for php

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages