Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Last active July 22, 2017 15:20
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ocramius/26a78f655e486718f858 to your computer and use it in GitHub Desktop.
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
vendor
composer.lock
{
"autoload": {
"classmap": [
"PissOff.php"
]
}
}
<?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);
<?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()
{
}
}
<?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);
@rdohms
Copy link

rdohms commented Mar 10, 2016

Should be Piss::off() right now its redundant

@QuingKhaos
Copy link

👍

@rosstuck
Copy link

What would the Piss::on() method do?

@Ocramius
Copy link
Author

@rdohms yeah, but then your API endpoints would return Piss, which I'm sure they do in your case, but you'd probably not want your app to piss all over the place...

@Akii
Copy link

Akii commented Mar 10, 2016

how about PissOff::rly()

@rdohms
Copy link

rdohms commented Mar 10, 2016

then how about: PissOff::now() ?

@QuingKhaos
Copy link

PissOff::wanker() :trollface:

@Akii
Copy link

Akii commented Mar 10, 2016

Needs tests, then it's production ready.

@Akii
Copy link

Akii commented Mar 10, 2016

@rdohms and then PissOff::createFromFormat()?

@Ocramius
Copy link
Author

@patkar suggestion approved. Updating the gist.

@prolic
Copy link

prolic commented Mar 10, 2016

lol

@emacstheviking
Copy link

Too much code. I always wanted to add a new function to PHP, 'motherfucker', which when used as an argument would cause random file system activity, hopefully deletion.

This code would then truly express my intent when PHP does my head in:

die(motherfucker(die)));

The more die values, the more the file system suffers...

@heiglandreas
Copy link

I'd return a new PissOff() in fortunately_you_can_tell_your_users_to_piss_off() to reduce the redundancy.

And may I suggest a different naming perhaps? https://youtu.be/qJMqdTr7IQM

@olekhy
Copy link

olekhy commented Mar 10, 2016

omg

@RWOverdijk
Copy link

I'm a wanker and I find this offensive

@Ocramius
Copy link
Author

@RWOverdijk the fact that you are not a minority does make that comment moot :D

@emacstheviking
Copy link

Where i work, we all have this in our shell startups:

alias fuckit="sudo"
alias I_quit="rm -fr /"

Then, one day, when it's all too much you can just do

$ fuckit I_quit

and start walking....

@imreg
Copy link

imreg commented Mar 10, 2016

Version number ?
Release date ?

maybe a Conference about it?
T-Shirt & Mug ... Company, called Piss Off Ltd

@eventhorizonpl
Copy link

@emacstheviking

You need to add --no-preserve-root

@carlosvini
Copy link

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;

@olekhy
Copy link

olekhy commented Mar 10, 2016

@Ocramius he he tends to one from django/django#2692

@bobmagicii
Copy link

Ocramius for President 2016

@Ocramius
Copy link
Author

@jp7carlos that is AWESOME!

@Bilge
Copy link

Bilge commented Mar 10, 2016

You have code duplication on lines 16 and 21. I honestly expected better.

@Ray-Paseur
Copy link

This just made my day!

@natenolting
Copy link

lol, oh you guys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment