PHP Classes

File: demo.php

Recommend this page to a friend!
  Classes of wim niemans   Armory PHP Closure Trait   demo.php   Download  
File: demo.php
Role: Example script
Content type: text/plain
Description: Armory Demo
Class: Armory PHP Closure Trait
Cache, rename and hide anonymous functions
Author: By
Last change:
Date: 3 years ago
Size: 2,377 bytes
 

Contents

Class file image Download
<?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
 */
 
?>