Skip to content

Instantly share code, notes, and snippets.

@xthiago
Last active August 29, 2015 14:15
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 xthiago/d9935804cbb555bd4720 to your computer and use it in GitHub Desktop.
Save xthiago/d9935804cbb555bd4720 to your computer and use it in GitHub Desktop.
Add support for instanceof in Twig
<?php
namespace Xthiago\CoreBundle\Twig;
class InstanceofExtension extends \Twig_Extension
{
/**
* {@inheritdoc}
*/
public function getTests()
{
return array(
new \Twig_SimpleTest('instanceof', array($this, 'isInstanceOf'))
);
}
public function isInstanceOf($object, $class)
{
if (!is_object($object))
return false;
$reflectionClass = new \ReflectionClass($class);
return $reflectionClass->isInstance($object);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'xthiago_instanceof_extension';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment