1. Go to this page and download the library: Download sqonk/phext-context 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/ */
sqonk / phext-context example snippets
use \sqonk\phext\context\context;
static public function file(string $filePath, string $mode = 'r')
// output some sample text to the file 'out.txt'.
context::file('out.txt', 'w')->do(function($fh) {
fwrite($fh, "This is a test");
});
static public function tmpfile()
// output some sample text to the temp file 'out.txt'.
context::tmpfile()->do(function($fh) {
fwrite($fh, "This is a test");
rewind($fh);
println('contents:', fread($fh, 50));
});
static public function stream(string $filePath, int $chunkMultiplier = 1024)
context::stream('path/to/large/file.mov')->do(function($buffer) {
println(strlen($buffer)); // print out each chunk of data read from the input stream.
});
static public function image(string $filePath)
/*
Open an image, modify it and output the result.
*/
context::image('/path/to/image.jpg')->do(function($img) {
# greyscale
imagefilter($img, IMG_FILTER_GRAYSCALE);
# pixelate
imagefilter($img, IMG_FILTER_PIXELATE, 8, IMG_FILTER_PIXELATE);
# output result
imagepng($img, 'modifiedImage.png');
});
static public function new_image(int $width, int $height)
/*
Create a new image, draw to it and output the result.
*/
context::new_image(500, 500)->do(function($img) {
# white background with a black square.
imagefilledrectangle($img, 0, 0, 499, 499, imagecolorallocate($img,255,255,255));
imagefilledrectangle($img, 220, 220, 320, 320, imagecolorallocate($img,0,0,0));
# output to file.
imagepng($img, 'out.png');
});
static public function supress_errors()
/*
The following block of code throws an exception which is caught
and ignored, leaving the program uninterupted.
Also note the use of while() on the callback. 'while' is an alias of 'do'
and with some context managers makes more syntactic sense.
*/
context::supress_errors()->while(function() {
println('throwing an exception');
throw new Exception('This is a test exception.');
});
static public function no_output()
static public function captured_output()
$output = context::captured_output()->do(function() {
// Print some text to the output. As the context manager wraps the callback
// between ob_start() and ob_end_clean(), the output is captured and returned
// to the caller.
print 'This is a test.';
});
println($output);
// will print "This is a test."
static public function pdo_transaction(\PDO $connection)
/*
Execute a transaction on a PDO object. The context manager will initiate
the transaction before passing off the instance to the callback. Once completed
the transaction will be comitted.
If an exception is raised at any point then the transaction is rolled back.
*/
$pdo = ... // your PDO instance, set up as
static publlic function mysql_transaction(\mysqli $connection)
static public function curl(string $url = '')
php
static public function zip(string $filePath, $mode = \ZipArchive::CREATE | \ZipArchive::OVERWRITE)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.