-1

I have a function that writes it into a file.

public function write($type = "error", $mensaje = "") { 
    $this->file = fopen($this->filePath.$this->fileName, "a+"); 

    if ($this->file == null) { 
        trigger_error("Error: No file", E_USER_ERROR); 
    } else { 
        fwrite($this->file, $type." ".$mensaje."\n");
    } 
}

I need to call this function from another php file say index.php

function test() {
    write("testing done");
    echo "done";
}

I want the write function to be called from index.php asynchronously so the current execution doesn't stop or block if the write fails.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rajeshwar
  • 391
  • 1
  • 5
  • 19

1 Answers1

0

with phptreads ( http://php.net/manual/en/intro.pthreads.php ), something like

class writerThread extends Thread {
public function __construct($type="error",$message=''){
  $this->type=$type;
$this->message=$message;
}

public function run(){
   file_put_contents($filename,$this->type.$this->message);
}
}

$writer=new WriterThread("testing done");
$writer->start();
hanshenrik
  • 19,904
  • 4
  • 43
  • 89