- What does the lelyfoto/twig-instanceof package actually do?
- It adds an instanceof test to Twig so templates can check whether a value is an instance of a specified PHP class or interface. The package ships a Twig extension that registers the test, supports namespaced class names, and keeps type checks inside template conditionals. It is small and focused rather than a full framework integration.
- How do I install lelyfoto/twig-instanceof with Composer?
- Install it with composer require lelyfoto/twig-instanceof. You then register the provided Twig extension in whatever Twig environment your application uses. The package does not ship a Laravel service provider, config file, or facade, so registration is manual.
- Does lelyfoto/twig-instanceof work with Laravel?
- Not directly. Laravel's view layer is Blade, and Blade compiles to PHP, so @if ($model instanceof \App\Models\User) already works with no dependency. This package only makes sense in a Laravel app that already runs Twig through a bridge such as rcrowe/twigbridge or in legacy Twig code. If your application is Blade-first, the package adds no value.
- How do I register the instanceof Twig extension in Laravel TwigBridge?
- Add the extension class to the extensions array in config/twigbridge.php, or resolve the Twig environment from the container and call addExtension(). There is no Laravel-idiomatic entry point shipped with the package, so you own the wiring. Confirm the extension class name against the installed version before registering it.
- Which Twig versions does lelyfoto/twig-instanceof support?
- The package uses Twig node-level extension APIs, which are the most brittle kind and have changed across Twig 3.x releases. There is no published compatibility matrix or CI evidence of Twig 4 readiness, and Twig 4 is expected to break node-level extensions. Verify the installed Twig version in a spike before adopting it.
- Can I check namespaced PHP classes and interfaces with this Twig test?
- Yes. The test supports fully qualified namespaced class names and interface or parent-class matching, following normal PHP instanceof semantics. The tradeoff is that templates become coupled to class names that IDE and refactoring tools will not track. A namespace rename can produce a silent false instead of an error.
- Is the Twig instanceof test safe to use with the Twig sandbox?
- Treat this with caution. Expression-node extensions sit below Twig\Sandbox\SecurityPolicy, so if the test registers as a raw node rather than a named allowed test, it may bypass sandbox allowlists. String-driven class resolution can also trigger the autoloader and expose class names in exception messages. Review the extension code and test it under your sandbox policy before production use.
- What are better alternatives to using instanceof inside Twig templates?
- Prefer a view model or DTO that exposes a boolean such as isUser, a domain-level discriminator string, or a purpose-named Twig test like is_admin_user. These keep type dispatch in PHP and avoid hard-coded fully qualified class names in views. In Laravel, Blade's native instanceof check is simpler than adding Twig and this package.
- Is lelyfoto/twig-instanceof actively maintained and production ready?
- It has a low maintenance signal: roughly 8 stars, no dependents, one author, and MIT licensing. Version 1.3 added two first-time contributors and a return-type fix for a deprecation, which suggests the node code is being touched to track upstream changes, but there is no CI matrix or Twig 4 evidence. Treat the code as a sample to vendor and review rather than a dependency to trust blindly.
- How should I test instanceof conditionals for null values and proxy objects?
- PHP instanceof returns false for null without throwing, and it matches interfaces and parent classes. Doctrine proxies and lazy ghosts subclass the base entity, so an instanceof check against the base class can pass when you expected the proxy type. Write template tests with mocked objects covering null, interface matches, and proxy-like subclasses before relying on the test in production.