Skip to content

Instantly share code, notes, and snippets.

@bwoebi
bwoebi / text.md
Last active February 19, 2022 01:08
fread() on non-blocking sockets

Reading of non-blocking TCP sockets in PHP

As a short heads-up for those unfamiliar:

  • There is a PHP level buffer. Whenever more is actually read from the socket (default chunk size is 8192), than the user requests in his PHP code, the data is stored there.
  • There is an OS level buffer. There all incoming network data lands. The event loop only knows of that one and checks that one for being non-empty.

Trivial single read

The trivial reading function in PHP is fread($socket, $maxSize).

@bwoebi
bwoebi / simple-fiber.php
Created August 30, 2020 16:34
Simplest Fiber API with usage example
<?php
class Fiber {
public static Fiber $main;
public static function current(): Fiber;
public function __construct(callable $new);
public function continue();
}
@bwoebi
bwoebi / example.php
Last active June 1, 2016 17:51 — forked from chrisleavoy/example.php
Amp\wait LogicException
<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
function resolve($pluginName, $x)
{
$saneDefaults = [
'types' => \Amp\Dns\Record::A, 'server' => "8.8.8.{$x}", 'timeout' => 1000, 'tries' => 1,
'cache' => false
];
$result = (yield \Amp\Dns\resolve($pluginName, $saneDefaults));
@bwoebi
bwoebi / local_redir.php
Last active December 13, 2015 01:17
Local Aerys Redirect
<?php
$dispatcher = new class implements \Aerys\Middleware {
public function __invoke(\Aerys\Request $req, \Aerys\Response $res) {
/* dispatch */
if (/* $local_request === */ true) {
$started = false;
$oldireq = $req->getLocalVar("cgihandler.ireq");
$ireq = clone $oldireq;
$ireq->locals = [];
@bwoebi
bwoebi / gist:77759ace18b63a4ccdf2
Last active August 29, 2015 14:17
Generic typehints (Generator)
<?php
class Fetched {
# dummy class
}
class Fetcher {
private $promises;
private $stream;
private $watcher;
@bwoebi
bwoebi / typehints.md
Last active August 29, 2015 14:15
Do NOT work around weak typehints, but fix them

To start, the current version (0.2) of the scalar type hints RFC is the correct way to do it, if we really want to have two modes (whether declare(strict_types=1) or <?php strict doesn't matter a lot here).

Type hinting in general is useful, no question. We can guarantee in the function body what we types are working with. That avoids bad surprises like working with a non-numeric string when we expect an integer. Very nice.

But two modes? Why? What issue are we solving with strict typing?

Strict typing

Only equal parameters can be passed in. There also a "numeric" type hint was proposed, to solve the issue where we just want a number.

@bwoebi
bwoebi / acinclude.diff
Created January 23, 2015 22:02
Always check against optional flag
diff --git a/acinclude.m4 b/acinclude.m4
index a4d4d50..3f69968 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -1031,24 +1031,26 @@ dnl The extension on which it depends must also have been configured.
dnl See ADD_EXTENSION_DEP in win32 build
dnl
AC_DEFUN([PHP_ADD_EXTENSION_DEP], [
- am_i_shared=$[PHP_]translit($1,a-z_-,A-Z__)[_SHARED]
- is_it_shared=$[PHP_]translit($2,a-z_-,A-Z__)[_SHARED]
<?php
spl_autoload_register(function ($name) {
// $exp will be the name of the class without the \with\traitname part => array_splice
$exp = explode("\\", $name);
// serach from end
$index = array_search("with", array_reverse($exp, true));
if (!$index || $index - 1 == count($exp)) // also fail when value is 0
return false;
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index d789f3c..8fca9ae 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -2226,33 +2226,28 @@ void zend_resolve_class_name(znode *class_name TSRMLS_DC) /* {{{ */
/* }}} */
void zend_do_create_anon_class(znode *result TSRMLS_DC) { /* {{{ */
- char *class_name = NULL;
- int class_name_len = 0;
<?php
const CLASS_PUBLIC = "";
const CLASS_PROTECTED = "\0*\0";
const CLASS_PRIVATE = "\0stdClass\0";
function create_class ($class) {
$class = (object)$class;
foreach ($class as $prop)
if ($prop instanceof Closure)
$prop->bindTo($class);