Skip to content

epfremmer/php-the-good-parts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP The Good Parts

Build Status Coverage Status Scrutinizer Code Quality SensioLabsInsight

Make PHP JavaScript again

Overview

Adds support for JavaScript like prototypes on PHP classes. Now you can define prototypical methods on your PHP classes just like the magic of JavaScript. No more concrete class definitions! Now you can simply define that at runtime, or better yet, dynamically!

Installation

With Composer

composer config repositories.epfremme/php-the-good-parts vcs https://github.com/epfremmer/php-the-good-parts.git
composer require epfremme/php-the-good-parts

Examples

Basic Usage

class FooClass
{
    use Javascript;
}

FooClass::prototype()->my_method = function() {
    echo 'Hello JavaScript!!!';
    
    return $this; // $this equals the instance of FooClass
};

$foo = new FooClass();
$foo->my_method(); // outputs: 'Hello JavaScript!!!'

Runtime Definition

class FooClass
{
    use Javascript;
}

$foo = new FooClass();

if (!is_callable($foo->prototype()->my_method)) {
    $foo->prototype()->my_method = function() {
        echo 'Hello JavaScript!!!';
    };
}

$foo->my_method(); // outputs: 'Hello JavaScript!!!'

$bar = new FooClass();
$bar->my_method(); // still outputs: 'Hello JavaScript!!!'

External Scope Variables

class FooClass
{
    use Javascript;
}

$message = 'Hello JavaScript!!!';

FooClass::prototype()->my_method = function() use ($message) {
    echo $message;
};

$foo = new FooClass();
$foo->my_method(); // outputs: 'Hello JavaScript!!!'

Prototype Inheritance

class FooClass
{
    use Javascript;
}

class BarClass extends FooClass
{
    use Javascript;
}

FooClass::prototype()->my_method = function() {
    echo 'Hello JavaScript!!!';
};

$bar = new BarClass();
$bar->my_method(); // outputs: 'Hello JavaScript!!!' as defined on parent FooClass

Tests

PHPUnit bin/phpunit

About

Satirical prototypical javascript PHP implementation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages