Skip to content

Instantly share code, notes, and snippets.

View lloc's full-sized avatar
🏠
Working from home

Dennis Ploetner lloc

🏠
Working from home
View GitHub Profile
<?php
function xrange( $start, $limit, $step = 1 ) {
for ( $i = $start; $i <= $limit; $i += $step ) {
yield $i;
}
}
/*
* An array is never created or returned
@lloc
lloc / trait.php
Last active March 23, 2018 10:26
<?php
namespace wctrn\realloc;
trait Logger {
public function log_error( string $msg ) : bool {
return error_log( $msg );
}
}
<?php
try {
// Something throws an Exception or Error.
} catch ( Throwable $t ) {
// will match only in PHP 7
} catch ( Exception $e ) {
// will be reached in PHP 5
}
<?php
namespace wctrn\realloc;
require_once 'late-static-bindings-post.php';
class Page extends Post {
public function get_content() {
return 'Hello Turin!';
<?php
namespace wctrn\realloc;
class Post {
public static function init() {
return new static();
}
<?php
namespace wctrn\realloc;
class Post {
protected $content;
public function get_content() : string {
return $this->content;
<?php
namespace wctrn\realloc;
class Post {
protected $content;
public function set_content( string $content ) {
$this->content = $content;
<?php
require_once 'namespace-class.php';
// Namespace: Using
echo ( new \wctrn\realloc\Post() )->get_content(), PHP_EOL;
// Namespaces: Importing
use \wctrn\realloc\Post;
echo ( new Post() )->get_content(), PHP_EOL;
<?php
namespace wctrn\realloc;
class Post {
public function get_content() {
return 'Hello World!';
}
}
<?php
abstract class aFoo {
abstract function baz( int $foobar );
}
abstract class aBar extends aFoo {
// expected type of foobar is still int
abstract function baz( $foobar ) : int;
}