Does PHP support the RAII pattern? How?

前端 未结 4 1237
挽巷
挽巷 2020-12-17 16:34

Most resources on PHP never touch memory management because the language itself is pretty good at doing this for you. However, in PHP you often end up dealing with external

4条回答
  •  别那么骄傲
    2020-12-17 16:46

    Slightly offtopic: you can do a using-like pattern with lambdas. Like this:

    function WithFile($Name, $Func)
    {
        $File = fopen($Name, 'r');
        $r = $Func($File);
        fclose($File);
        return $r;
    }
    

    And then use it like this

    $FileHeader = WithFile('myfile', function($File) {return fread($File, 16);});
    

    Perfectly deterministic. That said, were there a terser syntax for lambdas...

提交回复
热议问题