Last active
July 22, 2017 15:20
-
-
Save Ocramius/26a78f655e486718f858 to your computer and use it in GitHub Desktop.
PissOff: a valid, polite and useful alternative to the `void` return type in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vendor | |
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"autoload": { | |
"classmap": [ | |
"PissOff.php" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace MyStuff; | |
require_once __DIR__ . '/vendor/autoload.php'; | |
function fortunately_you_can_tell_your_users_to_piss_off() : PissOff { | |
return PissOff::wanker(); | |
}; | |
var_dump(fortunately_you_can_tell_your_users_to_piss_off()->i_told_you_to_piss_off); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace MyStuff; | |
final class PissOff | |
{ | |
/** | |
* @var \LogicException | |
*/ | |
private $pissOff; | |
public static function __callStatic(string $method, array $args) | |
{ | |
throw new \LogicException('You are not supposed to interact with this object: piss off.'); | |
} | |
private function __construct() | |
{ | |
$this->pissOff = new \LogicException('You are not supposed to interact with this object: piss off.'); | |
} | |
public static function wanker() : self | |
{ | |
return new self(); | |
} | |
public function __call(string $method, array $args) | |
{ | |
throw $this->pissOff; | |
} | |
public function __get(string $name) | |
{ | |
throw $this->pissOff; | |
} | |
public function __set(string $name, $value) | |
{ | |
throw $this->pissOff; | |
} | |
public function __isset(string $name) | |
{ | |
throw $this->pissOff; | |
} | |
public function __unset(string $name) | |
{ | |
throw $this->pissOff; | |
} | |
private function __wakeup() | |
{ | |
} | |
private function __sleep() | |
{ | |
} | |
private function __clone() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace MyStuff; | |
require_once __DIR__ . '/vendor/autoload.php'; | |
function doesnt_it_suck_that_php_has_no_void_return_type() { | |
}; | |
var_dump(doesnt_it_suck_that_php_has_no_void_return_type()->yeah_it_sucks); |
You need to add --no-preserve-root
This version won't even let you assign it to a variable (made it really fast and it's just a joke):
<?php
class Untouchable {
private static $count;
private static $register = [];
private $id;
public static function create()
{
return new self(self::$count++);
}
public static function check()
{
echo 'Checking...'.PHP_EOL;
if (count(self::$register)) {
throw new Exception('Can\'t touch this');
}
}
private function __construct($id)
{
$this->id = $id;
self::$register[$this->id] = true;
}
public function __destruct()
{
echo 'I was destroyed: '.$this->id.PHP_EOL;
unset(self::$register[$this->id]);
}
}
declare(ticks=1);
register_tick_function([Untouchable::class, 'check']);
function foo(): Untouchable {
return Untouchable::create();
}
foo();
$a = foo(); // Cant touch this
echo 'end of script'.PHP_EOL;
@Ocramius he he tends to one from django/django#2692
Ocramius for President 2016
@jp7carlos that is AWESOME!
This just made my day!
lol, oh you guys
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version number ?
Release date ?
maybe a Conference about it?
T-Shirt & Mug ... Company, called Piss Off Ltd