note 119135 added to class.closure
| From: | joe dot scylla at gmail dot com | Date: | Fri, 08 Apr 2016 08:46:56 +0000 |
| Subject: | note 119135 added to class.closure | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205181@lists.php.net to get a copy of this message | ||
Small little trick. You can use a closures in itself via reference.
Example to delete a directory with all subdirectories and files:
<?php
$deleteDirectory = null;
$deleteDirectory = function($path) use (&$deleteDirectory) {
$resource = opendir($path);
while (($item = readdir($resource)) !== false) {
if ($item !== "." && $item !== "..") {
if (is_dir($path . "/" . $item)) {
$deleteDirectory($path . "/" . $item);
} else {
unlink($path . "/" . $item);
}
}
}
closedir($resource);
rmdir($path);
};
$deleteDirectory("path/to/directoy");
?>
----
Server IP: 72.52.91.14
Probable Submitter: 213.164.67.62
----
Manual Page -- http://php.net/manual/en/class.closure.php
Edit -- https://master.php.net/note/edit/119135
Del: integrated -- https://master.php.net/note/delete/119135/integrated
Del: useless -- https://master.php.net/note/delete/119135/useless
Del: bad code -- https://master.php.net/note/delete/119135/bad+code
Del: spam -- https://master.php.net/note/delete/119135/spam
Del: non-english -- https://master.php.net/note/delete/119135/non-english
Del: in docs -- https://master.php.net/note/delete/119135/in+docs
Del: other reasons-- https://master.php.net/note/delete/119135
Reject -- https://master.php.net/note/reject/119135
Search -- https://master.php.net/manage/user-notes.php