drupol/anonymize

Converts an object/class into an anonymous class.

1.0.3 2018-02-27 07:59 UTC

README

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

Anonymize

Description

Convert a regular class into an anonymous class.

Features

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

Requirements

  • PHP >= 7.0

Installation

composer require drupol/anonymize

Usage

Using the object:

<?php

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!

API

/**
 * Convert an object into an anonymous object.
 *
 * @param $object
 *   The object to convert.  
 *
 * @return Anonymize
 */
AnonymizeTrait::convertToAnonymous($object);

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

Contributing

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

Sponsors