Skip to content
Dynamic objects a-la Javascript in PHP with inheritance
PHP
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
src
.gitignore
composer.json
example.php
readme.md

readme.md

fn - dynamic objects a-la Javascript in PHP

Previous version is here https://github.com/ptrofimov/jslikeobject/tree/1.0

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.