PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Konstantin S. Budylov   PHP Callback   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: example file
Class: PHP Callback
Register and invoke callback functions
Author: By
Last change: Example has been updated
Date: 18 years ago
Size: 3,754 bytes


Class file image Download
   Example file for class phpCallback

   This class is intended for management of 'callback' elements,
   such as Objects and it's methods, or arrays with it's values, or some functions,
   or whatever you want.

     It based on a principle of the mechanism of PHP 'call_user_func_array()' function,
   with preliminary addition of the 'callbacks' elements, and their subsequent call.

     For a calling a callback-elements, JAVA-syntax (array.object.method) is used as I find its more readable,
   in comparison with PHP syntax ($object->method()).
     If you think differently, simply change variables $separator
   in the functions get_array_key() and set_array_key(), stored
   in the file in this package (the values must be identical).

     Anyway, I find this class very convenient and useful for myself,
   and, if it will be useful for someone else - it will be pleasant for me. :)


require "class.phpCallback.php";

//Including class, which we'll use as a callback

$nwCallback = new phpCallback; //nwCallback object

           //First - we must create the objects, which we will use as a 'callback' elements
$example_callback =new example_callback;

//We assign a Object->method() as a callback


//Assign the Object->Property as a callback (property is exists)

//Assign the sample string
$nwCallback->AssignCallback("examples.strings.string1","This is 'string1'<br />");

//Assign the existent function (strtoupper() and getenv() in this example)

//Trying to call this functions...
      //We can transfer the parameters to the function in the second argument.
      //NOTE: Function will receive only one parameter (that type in which it's transferred),
echo $nwCallback->Execute("php.getenv","REMOTE_ADDR")."<br />";
$nwCallback->Execute("php.strtoupper","strtoupper-ed string")."<br />";

//Executing the example1::method1() with parameters (sample 'echo' of the parameter)
$nwCallback->Execute("examples.example1.method1","If you see this, this means, that the string <i>'examples.example1.method1'</i> is the callback <b>example1::method1()</b> trigger.<br />");

//This callback(example1::method2) will set the new property, given in second argument (array(property_name=>property_value))
      //(It makes a example1::method2(), but not my class:)
$nwCallback->Execute("examples.example1.method2",array("some_dynamical_property"=>"Property 'example1::some_new_property' is now exists"));

//So, let's check, that the property example1::some_dynamical_property is exists...
      //We adding the Object->Property

echo $nwCallback->Execute("examples.example1.some_new_property")."<br />"; //

      //Printing the example string was added at the top of script
echo $nwCallback->Execute("examples.strings.string1");

"<pre>" ; print_r($nwCallback); echo "</pre>";

     That's all.

     Not much, isn't so?