php-7instanceofprocesswire

PHP InstanceOf works locally but not on host server


I have an issue with PHP 7's instanceof statement that is only happening on certain conditions.

It seems that instanceof works locally on my dev machine (MAMP Pro running PHP 7.0.13) but not on my Hosted Server (HostEurope, PHP 7).

I have tried the following :

  1. downgrading to PHP 5.6
  2. using is_a instead
  3. Using fully qualified name e.g. \Site\Ad

but they all exhibit the same behaviour.

I've tried Googling "PHP instanceof not working" and variations of it but I haven't found anything relevant.

I was wondering if anyone had experienced something similar or possible solutions to try?

The Code in question is:

<?php

namespace Site;

require_once(__DIR__."/../interface/IOutput.php");
require_once(__DIR__."/../lib/Lib.php");
require_once(__DIR__."/../site/AdMediumRectangle.php");
require_once(__DIR__."/../site/AdSnapBanner.php");
require_once(__DIR__."/../const/Const.php");

class AdFactory
{

    /**
     * Define(AD_BANNER, 0);
     * Define(AD_RECTANGE, 1);
     * Define(AD_SUPERBANNER, 2);
     * Define(AD_SKYSCRAPER, 3);
     **/

    /**
     * @param $object
     * @return AdMediumRectangle|AdSnapBanner|string
     */
    public static function CreateObject($object)
    {
        $ad = wire(pages)->get("/ads/")->children->getRandom();

        if ($ad == null)
            return new \Exception("No Random Ad found");

        switch ($object) {
            case AD_BANNER:
                echo "AD_Banner Selected\r\n";
                $adSnapBanner = new AdSnapBanner($ad);
                return $adSnapBanner;
                break;
            case AD_RECTANGLE:
                echo "AD Rectangle Created\r\n";
                $adRectangle = new AdMediumRectangle($ad);
                return $adRectangle;
                break;
            case AD_SUPERBANNER:
            case AD_SKYSCRAPER:
            default:
                echo "AdFactory BlankObject created";
                return "";
                break;
        }
    }


    public static function Markup($object)
    {
        $obj = AdFactory::CreateObject($object);

        if (($obj instanceof AdSnapBanner) || ($obj instanceof AdMediumRectangle)) {
            echo "InstanceOf worked";
            return $obj->Markup();
        }
        else {
            echo "return blankString";
            return "";
        }
    }
}

Update : This is the code that calls the above AdFactory class

<?php

namespace Site;

require_once(__DIR__."/../interface/IOutput.php");
require_once(__DIR__."/../lib/Lib.php");
require_once(__DIR__."/../factory/AdFactory.php");
require_once (__DIR__."/../const/Const.php");

class AdInjector
{
    public static function Inject($page, $ad_pos)
    {
        //Select an Ad from /Ads/ according to criteria
        //$ads = wire(pages)->get("/ads/")->children;
        $count = 1; //$ads->count();

        if ($count > 0) {
            $mod = $page->id % 3;

            echo "mod=" . $mod . "\r\n";
            if ($mod == $ad_pos) {
                switch ($mod) {
                    case AD_POS_TITLE;
                    case AD_POS_BANNER:
                        //Pick an Snap Banner
                        echo "Banner Injected (banner):" . AD_BANNER . "\r\n";
                        return AdFactory::Markup(AD_BANNER);
                        break;
                    case AD_POS_SIBLINGS:
                        echo "Banner Injected (rect):" . AD_RECTANGLE . "\r\n";
                        //Pick an Ad Rectangle
                        return AdFactory::Markup(AD_RECTANGLE);
                        break;

                    default:
                        return "";
                        break;
                }
            } else
                return "";
        } else
            return "";
    }
}

Solution

  • Turns out there was a bug in 1 of the API calls I was making to the Processwire CMS.

    $ad = wire(pages)->get("/ads/")->children->getRandom();
    

    And my local and server instance of Processwire was not the same version, which was news to me. I normally have it synchronised, including any modules I use.

    I also suspect my null check is not correct PHP, to add to the problem.