中国青基会
RSS
热门关键字:  None  linux+moodle安装  mac  rhel5  199
当前位置 : Nixsky>程序设计>PHP>列表

PHP 中的批处理

来源:IBM DW中国 作者:Jack D. Herrington 时间:2007-03-03 点击:

mailouts 表一样,这两个表也由 PHP 类包装,这个类称为 ProcessingItems


清单 6. generic.php

				
<?php
require_once('DB.php');

class ProcessingItems
{
  public static function get_db() { ... }
  public static function delete( $id )
  {
    $db = ProcessingItems::get_db();
    $sth = $db->prepare( 'DELETE FROM processing_args WHERE item_id=?' );
    $db->execute( $sth, $id );
    $sth = $db->prepare( 'DELETE FROM processing_items WHERE id=?' );
    $db->execute( $sth, $id );
    return true;
  }
  public static function add( $function, $args )
  {
    $db = ProcessingItems::get_db();

    $sth = $db->prepare( 'INSERT INTO processing_items VALUES (null,?)' );
    $db->execute( $sth, array( $function ) );

    $res = $db->query( "SELECT last_insert_id()" );
    $id = null;
    while( $res->fetchInto( $row ) ) { $id = $row[0]; }

    foreach( $args as $key => $value )
    {
        $sth = $db->prepare( 'INSERT INTO processing_args
  VALUES (null,?,?,?)' );
        $db->execute( $sth, array( $id, $key, $value ) );
    }

    return true;
  }
  public static function get_all()
  {
    $db = ProcessingItems::get_db();

    $res = $db->query( "SELECT * FROM processing_items" );
    $rows = array();
    while( $res->fetchInto( $row ) )
    {
        $item = array();
        $item['id'] = $row[0];
        $item['function'] = $row[1];
        $item['args'] = array();

        $ares = $db->query( "SELECT key_name, value FROM
   processing_args WHERE item_id=?", $item['id'] );
        while( $ares->fetchInto( $arow ) )
            $item['args'][ $arow[0] ] = $arow[1];

        $rows []= $item;
    }
    return $rows;
  }
}
?>

这个类包含三个重要的方法:add()get_all()delete()。与 mailouts 系统一样,前端使用 add(),处理引擎使用 get_all()delete()

清单 7 所示的测试脚本将一个条目添加到处理队列中。


清单 7. generic_test_add.php

				
<?php
require_once 'generic.php';
ProcessingItems::add( 'printvalue', array( 'value' => 'foo' ) );
?>

在这个示例中,添加了一个对 printvalue 函数的调用,并将 value 参数设置为 foo。我使用 PHP 命令行解释器运行这个脚本,并将这个方法调用放进队列中。然后使用以下处理脚本运行这个方法。


清单 8. generic_process.php

				
<?php
require_once 'generic.php';

function printvalue( $args ) {
  echo 'Printing: '.$args['value']."\n";
}

foreach( ProcessingItems::get_all() as $item ) {
  call_user_func_array( $item['function'],
    array( $item['args'] ) );
  ProcessingItems::delete( $item['id'] );
}
?>

这个脚本非常简单。它获得 get_all() 返回的处理条目,然后使用 call_user_func_array(一个 PHP 内部函数)用给定的参数动态地调用这个方法。在这个示例中,调用本地的 printvalue 函数。

上一篇:没有了
下一篇:php分页处理
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册