*/ class CreateFile { /** * Thing we're creating * * @author Phil Burton */ protected $thing = "file"; protected $filename; protected $contents; /** * Check if the file already exists * * @param string $filename * @author Phil Burton */ public function __construct(string $filename, $contents = false) { $this->filename = $filename; if ($contents) { $this->contents = $contents; } if (!is_writable(dirname($filename))) { throw new \Exception('Cannot create ' . $this->thing . ' at: ' . $filename); } if (file_exists($filename)) { throw new \Exception('File already exists at: ' . $filename); } } /** * Create the new file * * @author Phil Burton */ public function execute() { touch($this->filename); $contents = $this->contents; if ($contents) { file_put_contents($this->filename, $contents); } } }