This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Converts an object/class into an anonymous class.

1.1.4 2019-07-23 18:50 UTC

This package is auto-updated.

Last update: 2021-07-15 01:34:32 UTC


Latest Stable Version GitHub stars Total Downloads Build Status Scrutinizer code quality Code Coverage Mutation testing badge License Say Thanks! Donate!



Convert a regular class into an anonymous class.


  • Converts public properties and methods into dynamic classes and properties.


  • PHP >= 7.1.3


composer require drupol/anonymize


Using the object:


include 'vendor/autoload.php';

class Hello
    public $property = 'YES!';

    public function say()
        echo 'Hello ' . $this->world();

    private function world()
        return 'world!';

$class = new Hello();
$class->say(); // Hello world!

$anonymizedClass = \drupol\Anonymize\Anonymize::convertToAnonymous($class);

$anonymizedClass::addDynamicMethod('say', function () use ($anonymizedClass) {
    echo 'Goodbye ' . $anonymizedClass->world();

$anonymizedClass::addDynamicMethod('world', function () {
    return 'universe!';

$anonymizedClass->say(); // Goodbye universe!



 * Convert an object into an anonymous object.
 * @param \stdClass $object
 *   The object to convert.  
 * @return Anonymize

The rest of the library API relies and inherit from DynamicObjects.

Code quality, tests and benchmarks

Every time changes are introduced into the library, Travis CI run the tests and the benchmarks.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

Before each commit some inspections are executed with GrumPHP, run ./vendor/bin/grumphp run to check manually.

PHPInfection is used to ensure that your code is properly tested, run composer infection to test your code.


Feel free to contribute to this library by sending Github pull requests. I'm quite reactive :-)