Advertisement
intval

Untitled

Oct 18th, 2011
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This class porpose is to execute a code once on the next request using the session as a place to register
  5.  * an instance of the class and execute the code when the object is being unserialized
  6.  *
  7.  *  Created by: Amit Dar
  8.  *  Website: http://www.i-Dev.co.il
  9.  */
  10. class NextRequestScheduler implements Serializable
  11. {
  12.     private $executable_function = null;
  13.     private static $session_var_name = 'next_request_scheduler';
  14.  
  15.     public function __construct($function_name)
  16.     {
  17.         if(!function_exists($function_name))
  18.         {
  19.             throw new BadFunctionCallException("Passed argument is not a valid function name");
  20.         }
  21.        
  22.         $this->executable_function = $function_name;
  23.         $_SESSION[self::$session_var_name][] = $this;
  24.     }
  25.  
  26.  
  27.      
  28.      
  29.     /*@see Serializable::serialize() */
  30.     public function serialize()
  31.     {
  32.         return $this->executable_function;
  33.     }
  34.  
  35.     /* @see Serializable::unserialize()*/
  36.     public function unserialize($serialized)
  37.     {
  38.         call_user_func($serialized);
  39.         $this->executable_function = null;
  40.        
  41.         /* unfortunatelly it's impossible to clean up the $_SESSION variable
  42.          * from used instances, since $_SESSION array is not yet populated
  43.          * at the moment of the unserialize function's call.
  44.          *
  45.          * Either you clean it up in a later stage or never clean it up.
  46.          * Possibly, clean old instances out when constructing new instances
  47.          */
  48.        
  49.     }
  50.  
  51. }
  52.  
  53.  
  54.  
  55. /******************* USAGE ***********************/
  56.  
  57. $closures_dont_work = function() {  echo 'hola from closure'; };
  58. function say_hi() { echo 'hi'; }
  59. function say_hello() { echo 'hello'; }
  60.  
  61.  
  62. session_start();
  63. new NextRequestScheduler('say_hello');
  64. new NextRequestScheduler('say_hi');
  65.  
  66. //new NextRequestScheduler($closures_dont_work);
  67. /* The only way to use closures here is to fetch their source code
  68.  * store it in the session and execute it on the next request.
  69.  * Fetching closures source code is possible reading the file having the declaration
  70.  *
  71.  * A better way would be to invoke a member of a class (say user->get_new_forum_posts)
  72.  * and that would require reimplementing the "unserialize" function to use call_user_method
  73.  * rather then call_user_func.
  74.  *
  75.  * Adopt it carefully
  76.  */
  77.  
  78.  
  79.  
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement