dimanche 20 décembre 2020

static variable keeps reinitialized in laravel php controller

As the title said, I have a problem with initializing a static variable in controller class. The static variable keeps return to the value null after leaving the class function. here is the code do i do something wrong?

class getBooks extends Controller
{
    public static $process;
    public static $pipes;
    public static function initBar()
    {
        ob_implicit_flush(true);
        ob_end_flush();

        $cmd = 'zbarcam.exe  -q --raw';

        $descriptorspec = array(
            0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
            1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
            2 => array("pipe", "w")    // stderr is a pipe that the child will write to
        );
        flush();


        getBooks::$process = proc_open($cmd, $descriptorspec, getBooks::$pipes, NULL, NULL);
    }

    /**
     * This function is responsible for reading bar code.
     */
    function getBar()
    {
        if (getBooks::$process == NULL) {
            getBooks::initBar();
        }
        if (is_resource(getBooks::$process)) {
            stream_set_blocking(getBooks::$pipes[1], 0);
            if ($s = fgets(getBooks::$pipes[1])) {
                return $s;
            }
        }
        return NULL;
    }



Aucun commentaire:

Enregistrer un commentaire