Skip to content

Instantly share code, notes, and snippets.

@aanton
Created November 25, 2013 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aanton/7637573 to your computer and use it in GitHub Desktop.
Save aanton/7637573 to your computer and use it in GitHub Desktop.
Null PHP Class
<?php
class Null
{
private static $instance;
public function __call($name, $arguments)
{
if (strpos($name, 'is') === 0 || strpos($name, 'has') === 0)
{
return false;
}
else
{
return self::getInstance();
}
}
function __toString()
{
return '';
}
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new Null();
}
return self::$instance;
}
}
/* Demo */
$null = Null::getInstance();
var_dump($null->isActive());
var_dump($null->hasImage());
var_dump($null->getImage());
var_dump($null->getImage()->getChildImage());
var_dump($null->getImage()->getChildImage()->isActive());
echo $null->isActive() . "\n";
echo $null->hasImage() . "\n";
echo $null->getImage() . "\n";
echo $null->getImage()->getChildImage() . "\n";
echo $null->getImage()->getChildImage()->isActive() . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment