PHP 5.5+ factory runtime class compiler
recompilr uses a class definition and recompiles it at runtime using eval and a unique hash identifier. Classes may be recompiled after changes are made to the class definition without requiring the application to restart. This effectively allows an application to redeclare classes at runtime.
Install via cli using composer:
php composer.phar require jgswift/recompilr:0.1.*
Install via composer.json using composer:
{
"require": {
"jgswift/recompilr": "0.1.*"
}
}
- php 5.5+
- adlawson\veval.php
// # path/to/FooClass.php
class FooClass {
/* */
}
// compiles FooClass from given class definition file
recompilr\execute('FooClass','path/to/FooClass.php');
// factory creates an instance of FooClass
$foo = recompilr\make('FooClass');
var_dump($foo); // (object) FooClass_*hash
// class must be available to autoloader
namespace MyNamespace;
class FooClass {
/* */
}
// compiles FooClass without an explicit class file, relying on the autoloader to find the class definition
recompilr\execute('MyNamespace\FooClass');
// factory creates an instance of FooClass
$foo = recompilr\make('MyNamespace\FooClass');
var_dump($foo); // (object) FooClass_*hash
When class definitions are expected to have changed, all classes may be recompiled using recompilr\all
.
// change path/to/FooClass.php while application is running
recompilr\all();
Note: will not compile files where bracketed namespaces are used
Note: all compiled classes are final and may not be inherited from
recompilr\execute('MyNamespace\FooClass');
recompilr\binary('path/to/binary.rcx');
recompilr\load('path/to/binary.rcx');
$foo = recompilr\make('MyNamespace\FooClass');
var_dump($foo); // (object) FooClass_*hash