Created
November 18, 2012 09:13
-
-
Save markwu/4104338 to your computer and use it in GitHub Desktop.
PHP: Universal Null Object
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 | |
/** | |
* Universal NULL object. | |
* | |
* Will throw no error messages so can be used as universal parameter. | |
* Does overwrite all available magic methods used in PHP5.3. | |
* | |
* @author Julius Beckmann | |
*/ | |
class NullObject { | |
public function __construct() {} | |
public function __destruct() {} | |
public function __set($name, $value) {} | |
public function __get($name) { return null; } | |
public function __isset($name) { return false; } | |
public function __unset($name) {} | |
public function __call($name, $args) {} | |
public function __toString() { return ''; } | |
public function __invoke() {} | |
public function __clone() {} | |
public static function __callStatic($name, $args) {} | |
} | |
/* | |
// Testcode | |
$null = new NullObject('foo'); | |
var_dump($null); | |
var_dump($null->a = 42); | |
var_dump($null->a); | |
var_dump(isset($null->a)); | |
unset($null->a); | |
var_dump($null->a('bar')); | |
var_dump($null::a('bar')); | |
var_dump((string)$null); | |
var_dump($null('foobar')); | |
var_dump(clone $null); | |
unset($null); | |
die(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment