Skip to content

Instantly share code, notes, and snippets.

View brzuchal's full-sized avatar

Michał Marcin Brzuchalski brzuchal

View GitHub Profile
@brzuchal
brzuchal / example.php
Created July 3, 2023 11:22
Interface Default Method Usecase
<?php
/**
* @template T
*/
class EntityResult {
public readonly $hasNextPage;
public function __construct(
public readonly array $entities,
public readonly int $page,
public readonly int $perPage,
@brzuchal
brzuchal / php.rfc
Last active March 19, 2021 20:03
PHP Structs Example
====== PHP RFC: Structs ======
* Version: 0.9
* Date: 2020-03-25
* Author: Michał Brzuchalski <brzuchal@php.net>
* Status: Draft
* First Published at: http://wiki.php.net/rfc/structs
Structures as a complex type solution for programmers in functional programming appears in few programming languages but differs in a solution taken there. Some languages like **C#** have both **classes** and **scructs** living aside in harmony.
@brzuchal
brzuchal / TestFrameworkInATweet.php
Created November 9, 2017 09:38 — forked from mathiasverraes/TestFrameworkInATweet.php
A unit testing framework in a tweet.
<?php function it($m,$p){echo"\e[3".($p?"2m✔︎":"1m✘")."\e[0m It $m\n";if(!$p){$GLOBALS['e']=1;$d=debug_backtrace()[0];echo"ERROR {$d['file']}@{$d['line']}\n";}}register_shutdown_function(function(){echo"\e[1;3".(($e=@$GLOBALS['e'])?"7;41mFAIL":"2mOK")."\e[0m\n";die($e);});
@brzuchal
brzuchal / README.md
Created July 9, 2020 04:45
people.php.net/brzuchal

Passionate PHP Developer focused on microservices and PHP internals.

Current RFC's

StackFrame class

It's an alternative to debug_backtrace() with stack-trace frames as objects using less memory than arrays. Introduced class include two static methods which allows collecting frames from stacktrace:

  • StackFrame::getTrace(?int $limit = null, ?int $offset = null): array - collects all or portion of stacktrace
  • StackFrame::getFrame(int $index): StackFrame - retrieves a single frame from the stacktrace
@brzuchal
brzuchal / php-macros.md
Last active July 6, 2020 13:45
PHP Marco Candidates

PHP Macro

Call syntax

Macro call syntax in ABNF:

arg-name   = 1*ALPHA
arg        = [ arg-name ":" ] ( expr | statement )
arg-list   = [ arg [ "," ] ]
arg-list  /= arg "," arg-list
macro-name = 1*ALPHA
@brzuchal
brzuchal / syntax.php
Created July 4, 2020 05:57
PHP Language Syntax Improvement
<?php
# Proposal
// remove parens gives a clear sign that it's not a function
declare strict_types = 1;
// require parens in constructs allowed to be used like a function
false or print("always returns 1");
// exit and die never returns value like echo and __halt_compiler
root@284352ebf2f3:/src# make install
/bin/bash /src/libtool --mode=install cp ./stdng.la /src/modules
cp ./.libs/stdng.so /src/modules/stdng.so
cp ./.libs/stdng.lai /src/modules/stdng.la
PATH="$PATH:/sbin" ldconfig -n /src/modules
----------------------------------------------------------------------
Libraries have been installed in:
/src/modules
...
----------------------------------------------------------------------
@brzuchal
brzuchal / delegates.md
Last active March 31, 2020 13:57
PHP delegates

PHP RFC: Delegates

Declaration

To distinguish between classes and delegates a delegate keyword is used to precede delegate name. For FQN optimisations an additional use delegate clause should be placed in all imports and aliases block.

namespace MyApp;
@brzuchal
brzuchal / reflection.php
Created March 25, 2020 20:01
PHP Reflection in PHP Namespace
<?php
namespace {
class ReflectionClass extends \Php\ReflectionClass {}
class ReflectionType extends \Php\ReflectionTypeConstraint {}
class ReflectionNamedType extends \Php\ReflectionClassTypeConstraint {}
}
namespace Php {
abstract class ReflectionType implements Reflector {}
class ReflectionClass extends ReflectionType {}
class ReflectionTypeConstraint {}
@brzuchal
brzuchal / data.php
Last active August 26, 2019 13:51
Data type PHP
<?php
data Foo {
var string $foo; // if not set then uninitialized
var bool $bar = false;
}
$foo = new Foo
{
foo: "baz",