Code Snippets PHP

Short texts PHP 🧑‍💻 Code snippets PHP ✍️ Reading lists PHP 👀

Extending SplTempFileObject with a store method

class TmpFile extends SplTempFileObject { public function store(string $filename): void { file_put_contents($filename, $this->getContents()); } public function getContents(): ?string { $this->rewind(); ob_start(); $this->fpassthru(); $contents = ob_get_contents(); ob_end_clean(); return $contents; } } // Usage $filepath = __DIR__ . ‘/file.txt’; $file = new TmpFile(); $file->fwrite(‘Lorem dolor sit amet’); $file->fwrite(‘…’); $file->store($filepath);