Categories


Archives


Recent Posts


Categories


PHP Namespaces and `instanceof`

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Not strictly a Magento 2 thing, but if you have code that looks like this in your extension

namespace FooBazBar;
//...
if( $node instanceof SimpleXMLElement) {}

You’ll need to make sure that SimpleXMLElement is actually used in your current namespace.

namespace FooBazBar;
use SimpleXMLElement
//...
if( $node instanceof SimpleXMLElement) {}

Otherwise the instanceof operator will be looking for an instance of FooBazBarSimpleXMLElement, and not SimpleXMLElement.

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 16th January 2016