Skip to content
Dynamic objects a-la Javascript in PHP with inheritance
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

fn - dynamic objects a-la Javascript in PHP

Previous version is here

So, well... fn is a magic function that transforms PHP anonymous function into function-constructor a-la Javascript. Like in JS, this function is an object and it could instantiate new objects on the own base.

$constructor = fn(function () {        // function-constructor
    $this->property = 1;               // add property
    $this->getProperty = function () { // add method
        return $this->property;

$instance = $constructor();            // create new instance

echo $instance->getProperty();         // result: 1
Something went wrong with that request. Please try again.