Skip to content

Instantly share code, notes, and snippets.

View zlikavac32's full-sized avatar
💭
7 8 9, but why?

Marijan Šuflaj zlikavac32

💭
7 8 9, but why?
View GitHub Profile
@zlikavac32
zlikavac32 / signal-handler-segfault.php
Created February 21, 2019 20:18
PHP segfault due to missing argument in printf
<?php
declare(strict_types=1);
pcntl_async_signals(true);
function printNow(): void {
echo (new DateTime())->format('Y-m-d H:i:s'), "\n";
//echo (new DateTime())->getTimestamp(), "\n"; // no segfault
//$dt = (new DateTime())->format('Y-m-d H:i:s'); echo $dt, "\n"; // no segfault
<?php
/**
* In this example, we'd use it like throw new FileNotFoundException($missingFileName)
* or throw new FileNotFoundException(sprintf("File not found")) or
* throw new FileNotFoundException(sprintf("File %s not found", $missingFileName))
* which quickly leads to non-uniform messages and hard-to-process/recover exceptions.
*
* Is it possible to perhaps send nice e-mail message?
*/
<?php
/**
* @method static WorldSide NORTH
* @method static WorldSide SOUTH
* @method static WorldSide EAST
* @method static WorldSide WEST
*/
abstract class WorldSide extends \Zlikavac32\Enum\Enum
{
<?php
/**
* @method static YesNo YES
* @method static YesNo NO
*/
abstract class YesNo extends \Zlikavac32\Enum\Enum
{
protected static function enumerate(): array
{
<?php
final class YesNo extends \Eloquent\Enumeration\AbstractEnumeration {
const NO = 0;
const YES = 1;
}
var_dump(YesNo::YES()->key()); // YES
<?php
class YesNo extends \SplEnum
{
const __default = self::YES;
const NO = 0;
const YES = 1;
}
$no = new YesNo(YesNo::NO);
<?php
interface UserFactory {
public function create(
string $email,
int $gender,
int $status
): User;
}
<?php
class User {
const GENDER_MALE = 0;
const GENDER_FEMALE = 1;
const STATUS_INACTIVE = 0;
const STATUS_ACTIVE = 1;
}
@zlikavac32
zlikavac32 / enum.php
Last active October 23, 2017 22:29
WIP - better enums in PHP (Java inspired)
<?php
declare(strict_types=1);
/**
* Proof of concept for better enums in PHP. Supports type-hinting and per enum abstract method implementation
*/
abstract class Enum
{
private static $existingEnums = [];
<?php
//...
public function testThatOnMondayItWorks(): void {
try {
timecop_travel(new DateTime('2017-08-28 00:00:00')); //Monday
$output = $this->runCommand('some-command');