Skip to content

Instantly share code, notes, and snippets.

@kl3sk
Forked from adrienbrault/extension.php
Last active July 24, 2020 08:58
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 kl3sk/ced7d082506e11783ac084d698759d6f to your computer and use it in GitHub Desktop.
Save kl3sk/ced7d082506e11783ac084d698759d6f to your computer and use it in GitHub Desktop.
instanceof twig test - SF 5

This extention check for instance object. It check if $object is a real object before reflect the clas itself.

<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigTest;
class InstanceOfExtension extends AbstractExtension
{
public function getTests()
{
return array(
new TwigTest('instanceof', array($this, 'isInstanceOf'))
);
}
public function isInstanceOf($object, $class)
{
if (!is_object($object)) {
return false;
}
$reflectionClass = new \ReflectionClass($class);
return $reflectionClass->isInstance($object);
}
}
{% if object is instanceof('Acme\\Foo\\Entity') %}
<p>true</p>
{% else %}
<p>false</p>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment