PHP Classes

File: Delegate.php

Recommend this page to a friend!
  Classes of sdwrage   Delegate   Delegate.php   Download  
File: Delegate.php
Role: Class source
Content type: text/plain
Description: Delegate Class File
Class: Delegate
Manage and call closures and callback functions
Author: By
Last change:
Date: 14 years ago
Size: 1,697 bytes
 

Contents

Class file image Download
<?php
/**
 * Delegate Class
 *
 * Class for creating a delegate
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to version 3.01 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_01.txt. If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @author Justin Lonas <sdwrage@gmail.com>
 * @copyright 2009 Critikal Studios
 * @version SVN: $Id$
 */
 
 
class Delegate
 
{
    protected
$_closures = array();
   
   
/*
     * Adds a closure to the list of closures
     *
     * @param array|string $closures
     */
   
public function add($closures)
    {
        if (
is_array($closures)) {
            if (
get_class($closures[0]) != 'Closure') { // Tests to see if its a class AND NOT a closure
               
$this->setClosure($closures);
            } else {
                foreach(
$closures as $closure) {
                   
$this->setClosure($closure);
                }
            }
        } else {
           
$this->setClosure($closures);
        }
    }
   
   
/*
     * Adds a single closure
     * @param array|string $closure
     */
   
public function setClosure($closure) {
        if (!
in_array($closure, $this->_closures)) {
           
$this->_closures[] = $closure;
        }
    }
   
   
/*
     * Executes each closure
     */
   
public function execute()
    {
        foreach(
$this->_closures as $closure) {
           
call_user_func($closure);
        }
    }
 }
?>