PHP Classes

Object Box: Process events with collections of objects

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: 105 All time: 9,729 This week: 206Up
Version License PHP version Categories
objbox 1.0Public Domain5PHP 5, Data types
Description 

Author

This package can process events with collections of objects.

There is a container class that can hold a collection of objects treating it like an array.

The container class can invoke custom functions to all objects in the collection, sort the objects, dispatch events using an object handler class, and perform all other operations typical of arrays.

Picture of Jeremy Stansfield
  Performance   Level  
Name: Jeremy Stansfield <contact>
Classes: 7 packages by
Country: United States United States

Example

<?php
include 'objbox.class.php';
include
'objhandler.class.php';
// small debug helper function
function debug($var) { echo "<pre>".stripslashes(var_export($var,true))."</pre>"; }

// an example event handler function
function event_handler( event $ev ) {
 
debug($ev);
}

// set the evenet handler
objhandler::set_event_handler('event_handler');

// an example object to be passed around
class passobj {
  public
$var = 0;
}

// an axample object number 1
class example1 {

  public function
hook($obj) {
   
debug("example1");
   
$obj->var++;

  }
  public function
hook_with_exception() {
    throw new
exception('some bad has happened.');
  }

}

// an example object number 2
class example2 {

  public function
hook($obj) {
    echo
debug("example2");
   
$obj->var++;
  }
  public function
hook_with_exception() {
   
// nothing bad happened here
    //throw new exception('some bad has happened.');
 
}
}

// create the new container
$box = new objbox();

// add some objects
$box['e1'] = new example1();
$box[] = new example2();
debug($box);

$var = new passobj();
debug($var);

$box->hook($var); // var plus 2 | 2 Events are fired

debug($var);

// access directly like this
$box['e1']->hook($var); // var plus 1 | Event is fired
// or like this
$box->e1->hook($var); // var plus 1 | Event is fired

debug($var);

$box->hook_with_exception(); // 3 events are fired, to calls, and 1 exception


  Files folder image Files (4)  
File Role Description
Plain text file objbox.class.php Class Object Box Class file
Plain text file objhandler.class.php Class Object Wrapper
Accessible without login Plain text file example.php Example Example usage script
Accessible without login Plain text file example.output.txt Output Output of example.php

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:105
This week:0
All time:9,729
This week:206Up