Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@patie patie commented Jan 5, 2014

+1 :)

@xthiago

This comment has been minimized.

Copy link

@xthiago xthiago commented Dec 17, 2014

👍 😃

@gaetanhauray

This comment has been minimized.

Copy link

@gaetanhauray 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
You can’t perform that action at this time.