1. Go to this page and download the library: Download catpaw/unsafe library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use CatPaw\Unsafe\Unsafe;
use function CatPaw\Unsafe\anyError;
use function CatPaw\Unsafe\error;
use function CatPaw\Unsafe\ok;
// This is not public function __toString() {
return "I'm looking for $this->fileName, where's the file Lebowski????";
}
}
/**
* Attempt to open a file.
* @param string $fileName
* @return Unsafe<resource>
*/
function openFile(string $fileName){
if(!file_exists($fileName)){
return error(new FileNotFoundError($fileName));
}
if(!$file = fopen('file.txt', 'r+')){
return error("Something went wrong while trying to open file $fileName.");
}
return ok($file);
}
/**
* Attempt to read 5 bytes from the file.
* @param resource $stream
* @return Unsafe<string>
*/
function readFile($stream){
$content = fread($stream, 5);
if(false === $content){
return error("Couldn't read from stream.");
}
return ok($content);
}
/**
* Attempt to close the file.
* @param resource $stream
* @return Unsafe<void>
*/
function closeFile($stream){
if(!fclose($stream)){
return error("Couldn't close file.");
}
return ok();
}
// open file
$file = openFile('file.txt')->try($error);
if ($error) {
echo $error.PHP_EOL;
die();
}
// read contents
$contents = readFile($file)->try($error);
if ($error) {
echo $error.PHP_EOL;
die();
}
// close file
closeFile($file)->try($error);
if ($error) {
echo $error.PHP_EOL;
die();
}
echo $contents.PHP_EOL;
$contents = anyError(function() {
// open file
$file = openFile('file.txt')->try($error)
or yield $error;
// read contents
$contents = readFile($file)->try($error)
or yield $error;
// close file
closeFile($file)->try($error)
or yield $error;
return $contents;
})->try($error);
if($error){
echo $error.PHP_EOL;
die();
}
echo $contents.PHP_EOL;
if($error){
return error($error);
}
$result = anyError(/* ... */)->try($error) or match($error:class){
FileNotFoundError::class => $error->getMessage(),
default => "Let me explain something to you. Um, I am not Mr. Lebowski. You're Mr. Lebowski.",
};
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.