PHP Classes

Armory PHP Closure Trait: Cache, rename and hide anonymous functions

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 153 All time: 9,053 This week: 59Up
Version License PHP version Categories
armory 1.0.0Freeware7Language, Design Patterns, PHP 7, Traits
Description 

Author

This package provides a trait to cache, rename and hide your anonymous and callback functions in a class object instance.

It provides a protected array to store the functions under a different name.

By declaring the functions as properties of the class object, getter and setter methods take care of the hiding. The executing scope is preserved.

The trait can be used to gather all callback functions and store them in a protected variable to avoid name collisions.

It can also make class functions available without the necessity of retrieving the object instances first.

Innovation Award
PHP Programming Innovation award nominee
August 2020
Number 3
Some classes need to implement a callback system that allows applications to customize actions to be executed when certain events happen inside the class.

This package provides a trait that can be used to quickly add callback function registration and invocation for any number of types of events that a class may trigger.

Manuel Lemos
Picture of wim niemans
  Performance   Level  
Name: wim niemans <contact>
Classes: 6 packages by
Country: The Netherlands The Netherlands
Age: ???
All time rank: 345989 in The Netherlands The Netherlands
Week rank: 180 Up6 in The Netherlands The Netherlands Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php

include_once 'armory.php';

Class
Proxy
{
use
armory;

public function
__construct() { $this->name = 'proxy'; }
public function
sayHello() { echo 'Hello from class ' . __CLASS__ . " named {$this->name} \n"; }
}
Class
A
{
public function
__construct() { $this->name = 'A'; }
public function
sayHello() { return function() { echo 'Hello from class ' . __CLASS__ . " named {$this->name} as ".get_called_class()." \n";}; }
}
Class
B
{
public function
__construct() { $this->name = 'B'; }
public function
sayHello() { return function() { echo 'Hello from class ' . __CLASS__ . " named {$this->name} as ".get_called_class()." \n";}; }
}

function
globalFunction($text) { echo "Hello from global scope $text \n"; }

$p = new Proxy;
$a = new A;
$b = new B;
$text = 'none';

$p->a1 = $a->sayHello();
$p->b1 = $b->sayHello();
$p->p1 = [$p, 'sayHello'];
$p->g1 = 'globalFunction';
$p->g2 = function() use ($text) { return globalFunction($text); };

print_r($p);
print_r($a);
print_r($b);

$p->a1();
$p->b1();
$p->p1();
$p->g1('G1');
$p->g2('G2');

$a->name = 'AA';
$b->name = 'BB';
$p->name = 'same';

$p->a1();
$p->b1();
$p->p1();

/* Outputs:
Proxy Object
(
    [armory:protected] => Array
        (
            [name] => proxy
            [a1] => Closure Object
                (
                    [this] => A Object
                        (
                            [name] => A
                        )

                )

            [b1] => Closure Object
                (
                    [this] => B Object
                        (
                            [name] => B
                        )

                )

            [p1] => Array
                (
                    [0] => Proxy Object
 *RECURSION*
                    [1] => sayHello
                )

            [g1] => globalFunction
            [g2] => Closure Object
                (
                    [static] => Array
                        (
                            [text] => none
                        )

                )

        )

)
A Object
(
    [name] => A
)
B Object
(
    [name] => B
)
Hello from class A named A as A
Hello from class B named B as B
Hello from class Proxy named proxy
Hello from global scope G1
Hello from global scope none
Hello from class A named AA as A
Hello from class B named BB as B
Hello from class Proxy named same
 */
 
?>


  Files folder image Files (2)  
File Role Description
Plain text file armory.php Class Armory Trait
Accessible without login Plain text file demo.php Example Armory Demo

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:153
This week:0
All time:9,053
This week:59Up