Skip to content

Instantly share code, notes, and snippets.

@oleics
Last active August 9, 2016 15:08
Show Gist options
  • Save oleics/edc40ff0c2ed064b6874bd9a181261a1 to your computer and use it in GitHub Desktop.
Save oleics/edc40ff0c2ed064b6874bd9a181261a1 to your computer and use it in GitHub Desktop.
void_not_null
<?php
use \stdClass;
/*
```php
<?php
use VoidNotNullWorkaroundTrait;
class MyClass {
use VoidNotNullWorkaroundTrait;
}
MyClass::fakeVoidAndNullInit();
```
*/
trait VoidNotNullWorkaroundTrait {
static public function fakeVoidAndNullInit() {
self::fakeVoidInit();
self::fakeNullInit();
}
protected static $fakeVoid;
static public function fakeVoidInit() {
if(!isset(self::$fakeVoid)) {
self::$fakeVoid = new stdClass();
}
}
static public function &fakeVoid() {
return self::$fakeVoid;
}
static public function isFakeVoid(&$value) {
return ($value === self::$fakeVoid);
}
protected static $fakeNull;
static public function fakeNullInit() {
if(!isset(self::$fakeNull)) {
self::$fakeNull = new stdClass();
}
}
static public function &fakeNull() {
return self::$fakeNull;
}
static public function isFakeNull(&$value) {
return ($value === self::$fakeNull);
}
}
<?php
// Dear PHP, me not happy:
function a_VOID_NOT_NULL_returning_function() {}
echo empty(a_VOID_NOT_NULL_returning_function())."\n";
// prints 1
echo is_null(a_VOID_NOT_NULL_returning_function())."\n";
// prints 1
echo (null === a_VOID_NOT_NULL_returning_function())."\n";
// prints 1
echo (null !== a_VOID_NOT_NULL_returning_function())."\n";
// prints
echo (null == a_VOID_NOT_NULL_returning_function())."\n";
// prints 1
echo (null != a_VOID_NOT_NULL_returning_function())."\n";
// prints
echo (json_encode(a_VOID_NOT_NULL_returning_function()))."\n";
// prints null
echo (serialize(null))."\n";
// prints N;
echo (serialize(a_VOID_NOT_NULL_returning_function()))."\n";
// prints N;
// Reason: Promises.
// Hello "Workaround"! Hello "Never ending explanations"!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment