Skip to content

Instantly share code, notes, and snippets.

@adrienbrault
Created October 18, 2013 18:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save adrienbrault/7045544 to your computer and use it in GitHub Desktop.
Save adrienbrault/7045544 to your computer and use it in GitHub Desktop.
instanceof twig test
<?php
class LayoutExtension extends \Twig_Extension
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'yolo';
}
/**
* {@inheritdoc}
*/
public function getTests()
{
return array(
new \Twig_SimpleTest('instanceof', array($this, 'isInstanceOf'))
);
}
public function isInstanceOf($object, $class)
{
$reflectionClass = new \ReflectionClass($class);
return $reflectionClass->isInstance($object);
}
}
@patie
Copy link

patie commented Jan 5, 2014

+1 :)

@xthiago
Copy link

xthiago commented Dec 17, 2014

👍 😃

@gaetanhauray
Copy link

gaetanhauray commented Oct 20, 2017

Nice ! Thanks you for the ReflectionClass->isInstance() tips !
I just think you can remove getName() implementation because (from doc in \Twig_Extension) :
@deprecated since 1.26 (to be removed in 2.0), not used anymore internally

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