Runtime Classes. A experiment with PHP and Object Oriented Programming
Join the DZone community and get the full member experience.
Join For Free
last week i was thinking about creation of a new type of classes. php
classes but created dynamically at run time. when this idea was running
through my head i read the following
article
and i wanted to write something similar. warning: probably that it is
something totally useless, but i wanted to create a working prototype
(and it was fun to do it
). let’s start:
we are going to crate something like this:
class human { private $name; function __construct($name) { $this->name = $name; } function hello() { return "{$this->name} says hello"; } } $gonzalo = new human('gonzalo'); $peter = new human('peter'); echo $gonzalo->hello(); // outputs: gonzalo says hello echo $peter->hello(); // outputs: peter says hello
but without using class statement, adding the constructor and hello method dynamically.
function testsimpleusage() { $human = hclass::define() ->fct(hclass::__construct, function($self, $name) {$self->name = $name;}) ->fct('hello', function($self) { return "{$self->name} says hello"; }); $gonzalo = $human->create('gonzalo'); $peter = $human->create('peter'); $this->assertequals("gonzalo says hello", $gonzalo->hello()); $this->assertequals("peter says hello", $peter->hello()); }
i also want to test undefinded functions with an exception:
function testcallingundefinedfunctions() { $human = hclass::define() ->fct(hclass::__construct, function($self, $name) {$self->name = $name;}) ->fct('hello', function($self) { return "{$self->name} says hello"; }); $gonzalo = $human->create('gonzalo'); $this->setexpectedexception('exception', "error method 'goodbye' does not exits"); $gonzalo->goodbye(); }
and simple inheritance too
function testinheritance() { $human = hclass::define() ->fct(hclass::__construct, function($self, $name) {$self->name = $name;}) ->fct('hello', function($self) { return "{$self->name} says hello"; }); $shyhuman = hclass::define($human) ->fct('hello', function($self) { return "{$self->name} is shy and don't says hello"; }); $gonzalo = $human->create('gonzalo'); $peter = $shyhuman->create('peter'); $this->assertequals("gonzalo says hello", $gonzalo->hello()); $this->assertequals("peter is shy and don't says hello", $peter->hello()); }
now we are going to create dynamically functions:
function testdinamicallyfunctioncreation() { $human = hclass::define() ->fct(hclass::__construct, function($self, $name) {$self->name = $name;}) ->fct('hello', function($self) { return "{$self->name} says hello"; }); $gonzalo = $human->create('gonzalo'); $this->assertequals("gonzalo says hello", $gonzalo->hello()); try { $gonzalo->goodbye(); } catch (exception $e) { $this->assertequals("error method 'goodbye' does not exits", $e->getmessage()); } $human->fct('goodbye', function($self) { return "{$self->name} says goodbye"; }); $this->assertequals("gonzalo says goodbye", $gonzalo->goodbye()); }
and that’s it. it works. probably with php5.4 we can drop the “$self” variable. it’s an ugly trick to pass the real instance of the class to the callback, thanks to the “added closure $this support back” feature added in the new version of php. but at least now we need to it.
and now another turn the screw. let’s try to create the fizzbuzz kata with those “runtime classes”. i will create two versions of fizzbuzz. here you can see my implementation of both versions with “standard” php. whith a simple class, and another one with two classes and dependency injection. now using the experiment:
function testfizzbuzz() { $fizzbuzz = hclass::define() ->fct('run', function($self, $elems = 100) { list($fizz, $buzz) = array('fizz', 'buzz'); return array_map(function ($element) use ($fizz, $buzz) { $out = array(); if ($element % 3 == 0 || strpos((string) $element, '3') !== false ) { $out[] = $fizz; } if ($element % 5 == 0 || strpos((string) $element, '5') !== false ) { $out[] = $buzz; } return (count($out) > 0) ? implode('', $out) : $element; }, range(0, $elems-1)); }); $fizzbuzz = $fizzbuzz->create(); $arr = $fizzbuzz->run(); $this->assertequals(count($arr), 100); $this->assertequals($arr[1], 1); $this->assertequals($arr[3], 'fizz'); $this->assertequals($arr[4], 4); $this->assertequals($arr[5], 'buzz'); $this->assertequals($arr[6], 'fizz'); $this->assertequals($arr[20], 'buzz'); $this->assertequals($arr[13], 'fizz'); $this->assertequals($arr[15], 'fizzbuzz'); $this->assertequals($arr[53], 'fizzbuzz'); }
function testanotherfizzbuzzimplementationwithdependencyinjection() { $fizzbuzz = hclass::define(); $fizzbuzz->fct(hclass::__construct, function($self, $fizzbuzzelement) { $self->fizzbuzzelement = $fizzbuzzelement; }); $fizzbuzz->fct('run', function($self, $elems = 100) { $out = array(); foreach (range(1, $elems) as $elem) { $out[$elem] = $self->fizzbuzzelement->render($elem); } return $out; }); $fizzbuzzelement = hclass::define() ->fct('render', function($self, $element) { list($fizz, $buzz) = array('fizz', 'buzz'); $out = array(); if ($element % 3 == 0 || strpos((string) $element, '3') !== false ) { $out[] = $fizz; } if ($element % 5 == 0 || strpos((string) $element, '5') !== false ) { $out[] = $buzz; } return (count($out) > 0) ? implode('', $out) : $element; }); $fbe = $fizzbuzzelement->create(); $this->assertequals($fbe->render(1), 1); $this->assertequals($fbe->render(2), 2); $this->assertequals($fbe->render(3), 'fizz'); $this->assertequals($fbe->render(4), 4); $this->assertequals($fbe->render(5), 'buzz'); $this->assertequals($fbe->render(6), 'fizz'); $this->assertequals($fbe->render(20), 'buzz'); $this->assertequals($fbe->render(13), 'fizz'); $this->assertequals($fbe->render(15), 'fizzbuzz'); $this->assertequals($fbe->render(53), 'fizzbuzz'); $fb = $fizzbuzz->create($fbe); $arr = $fb->run(); $this->assertequals(count($arr), 100); $this->assertequals($arr[1], 1); $this->assertequals($arr[3], 'fizz'); $this->assertequals($arr[4], 4); $this->assertequals($arr[5], 'buzz'); $this->assertequals($arr[6], 'fizz'); $this->assertequals($arr[20], 'buzz'); $this->assertequals($arr[13], 'fizz'); $this->assertequals($arr[15], 'fizzbuzz'); $this->assertequals($arr[53], 'fizzbuzz'); }
and that’s all. as i said before probably this “hybrid classes” or “runtime classes” (i don’t know how to name them) are totally useless, but it’s fun to do it.
phpunit hclasstest.php phpunit 3.4.5 by sebastian bergmann. ...... time: 0 seconds, memory: 5.00mb ok (6 tests, 39 assertions)
full code on github
from http://gonzalo123.wordpress.com/2011/08/08/runtime-classes-a-experiment-with-php-and-object-oriented-programming/
Opinions expressed by DZone contributors are their own.
Comments