Skip to content

Instantly share code, notes, and snippets.

View kobus1998's full-sized avatar
😳
-10x programmer

Kobus kobus1998

😳
-10x programmer
View GitHub Profile
@kobus1998
kobus1998 / routes_example.php
Created December 29, 2020 11:55
php attributes example routes
<?php
#[attribute]
class Route
{
public $method;
public $path;
public function __construct($method, $path) {
@kobus1998
kobus1998 / csv_generator.php
Created December 17, 2020 12:55
generate a csv using a single function
<?php
/**
* @var array[]
* @var string
* @var string
*/
function csv($aCsv, $sDelimiter = ',', $sLineBreak = "\n")
{
return implode($sLineBreak, array_map(
@kobus1998
kobus1998 / flushExample.php
Created December 16, 2020 13:22
flush example
<?php
echo "Begin ...\n";
for( $i = 0 ; $i < 10 ; $i++ )
{
echo $i . "\n";
flush();
sleep(1);
}
echo "End ...\n";
@kobus1998
kobus1998 / Bcdec.php
Created November 24, 2020 15:24
bc math decimals wrapper
<?php
declare(strict_types=1);
/**
* wrapper around the bcdec extension
*
* @see https://www.php.net/manual/en/book.bc.php
*/
class Bcdec
@kobus1998
kobus1998 / simple_backtrace.php
Created November 20, 2020 15:05
simple backtrace formatter
<?php
/**
* clear overview of backtrace
*
* @return string
*/
function backTrace($traces = null)
{
$traces = $traces ?? debug_backtrace();
@kobus1998
kobus1998 / Singleton.php
Created November 16, 2020 12:01
Singleton extend class
<?php
class singleton
{
/**
* protect the construct
*/
protected function __construct()
{
}
@kobus1998
kobus1998 / LoggingMiddleware.php
Created November 10, 2020 12:18
log all outgoing requests using guzzle's handlers
<?php
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
class LoggingMiddleware
{
/** @var LoggerInterface */
private $logger;
@kobus1998
kobus1998 / performance-for-loop.php
Created October 19, 2020 12:06
this tests the performance of count inside the loop or outside of the loop
<?php
function test1($arr)
{
$arr = array_fill(0, 10000, true);
$start = microtime();
for ($i = 0; $i < count($arr); $i++) {
array_fill(0, 500, "abc");
}
@kobus1998
kobus1998 / index.php
Created August 14, 2020 09:56
use baberlei/assert library to internationalise your validation messages
<?php
/** @see https://github.com/beberlei/assert */
require __DIR__ . '/vendor/autoload.php';
use Assert\Assertion as A;
$sLang = 'en';
@kobus1998
kobus1998 / listener_with_priorities.php
Created August 13, 2020 10:02
event listener with priorities
<?php
class Event
{
public $priority;
public $call;
public $name;