PHP Prototyping part 2

Apr 21

I had quite a bit of fun working on the prototyping class but I’ve realized I’ve missed a lot of things. So I sat down today to expand it a bit. I added in accessing of parent class variables (Don’t know how I missed this one). I also added in the ability to assign a function to a variable to call it later. Lets look at some examples:

First I added JavaScript like loading of the functions, this

$test1 = new Prototype('DateTime');
$test1->setDate = function() use($test1) {
    $args = func_get_args();
    $test1->parentCall('setDate', $args);

Now we assign it to a variable:

$func = $test1->setDate;
$func(2011, 4, 21);
echo $test1->format('Y-m-d'); // 2012-04-21 since our expansion increments the year

// But it still works from the object itself
$test1->setDate(2021, 4, 21);
echo $test1->format('Y-m-d'); // 2022-04-21

Finally assigning non-extended functions to variables

$func2 = $test1->format;
echo $func2('Y-m-d'); // 2022-04-21

So I think the class is really shaping up. It still needs work and I’m still interested in your ideas, so feel free to let me know.

Here is the class itself now on BitBucket

Leave a Reply