File: demo/demo-class-backend.php

Recommend this page to a friend!
  Classes of Alexander Selifonov  >  PHP Long Running Operations  >  demo/demo-class-backend.php  >  Download  
File: demo/demo-class-backend.php
Role: Example script
Content type: text/plain
Description: Demo Backend (OOP)
Class: PHP Long Running Operations
Execute long operations split in small tasks
Author: By
Last change:
Date: 7 years ago
Size: 2,207 bytes


Class file image Download
* @name demo-backend.php - backend module for the demo.php
* Demonstration for class.longops.php
* @author Alexander Selifonov
* @license MIT


myLongOp extends LongProcess {
$curPos = 0;
$count = 0;

    public function
start($params=array()) { // perform initialization before main working loop
$this->curPos = 0;
$this->count = 100;
        return array(
'lastItem'=>$this->curPos, 'itemCount'=>$this->count);
    public function
resume($params=array()) { // restore state before continue main working loop
$this->curPos = isset($params['lastItem'])? $params['lastItem'] : 'xxx';
$params['itemCount'])) $this->count = $params['itemCount'];

    public function
cancel() { // cleanup code before cancel job
        # cleanup code before cancellng job
$ret = array('message'=>'You cancelled this job at '.date('H:i:s'));
    public function
saveState() {
# save current state before pausing, return 'message' if needed, to show it user
return array('lastItem'=>$this->curPos, 'itemCount'=>$this->count,'message'=>"done $this->curPos of $this->count");
    public function
action() { // perform single action in main working loop
$this->curPos ++;
// # advanced finish if you need it for some reason:
// if($this->curPos>40) return 'We have finished right now!';
return array('lastItem'=>$this->curPos, 'itemCount'=>$this->count);
    public function
finish() { // cleanup code before succsessful finish of the whole job
        # make cleanup procedures...
return array('lastItem'=>$this->curPos, 'itemCount'=>$this->count, 'message'=>'Finished at '.date('H:i:s'));


# $params = array_merge($_GET,$_POST);

# if(isset($params['longops_action'])) {
    # 'maxtime': we give 2 seconds for one working session
$myHandler = new myLongOp();
$longop = new LongOps($myHandler, array('processid'=>'LONGOPT_OOP', 'maxtime'=>2));
# }
# else die('Empty backend call (no longops_action passed) !');

For more information send a message to info at phpclasses dot org.