Hi there everyone!

I'm trying to include Mobile-Detect in my PHP script and for some reason, it can't find the class.
PHP 8.3
source: https://github.com/serbanghita/Mobile-Detect/releases/tag/4.8.06

My code:

// Third party inclusion for mobile detection
require_once ('./includities/Mobile-Detect/MobileDetect.php');
$detect = new MobileDetect;

Inside MobileDetect.php:

class MobileDetect
{
    /**
     * A cache for resolved matches
     *  Implementation of PSR-16: Common Interface for Caching Libraries
     *  https://www.php-fig.org/psr/psr-16/
     *
     * Replace this with your own implementation.
     */
    protected Cache $cache;

    /**
     * Stores the version number of the current release.
     */
    protected string $VERSION = '4.8.06';

On pageload:

Error: Class "MobileDetect" not found

I've tried installing this via composer as well as the current(and previously successful) inclusion of the src code but for some reason, this time I can't get this to work at all. Reading the documentation, I've tried the various methods of calling this but nothing I've tried seems to work.

What am I fudging?

    Are you sure the require_once is being called before the first time you try to do a new MobileDetect()? (Presumably the error message gives you a file and line number where it's trying to instantiate it?)

      What are your namespaces looking like? Looking at the MobileDetect source, the class you're loading is declared at the top of the file to be in a Detection namespace, which means the full name of the class you're instantiating is \Detection\MobileDetect; try using that. Alternatively, if you don't want to have to refer to it by its full name every time, add a use declaration use \Detection\MobileDetect as MobileDetect; (or as whatever you want to call the class for your convenience) at the top of your script.

      Alternatively alternatively (since you mention it working before and also mention composer) you may be missing an autoloader somewhere.

      Thank you both very much for your help. The only thing I can think of is that the composer install didn't go well and it was overriding my included package in my PHP code.

      Weedpacket which means the full name of the class you're instantiating is \Detection\MobileDetect; try using that.

      I did try that. In fact, I had gone through and tried every include that I found in documentation/search results (\Detection\MobileDetect, Mobile_Detect, MobileDetect) but I couldn't get the error to go away.

      During the process, I realized I didn't need that much code for what I want(just some very simple stats of site usage) so I am going to try to just write a small blob of code to give me some info based on the user agent string.

        Your problem here may also be due to the file permission problem you appear to be having with your JS and CSS files from your other thread.

        During the process, I realized I didn't need that much code for what I want(just some very simple stats of site usage) so I am going to try to just write a small blob of code to give me some info based on the user agent string

        Yup, in my experience, anyway MobileDetect is primarily for making sure that you show mobile resources to mobile devices and not to desktop devices. Although the line's gotten blurry since I last used it. We used to create separate "www.site.com" and "m.site.com" sites and use MobileDetect to set a cookie that sent the browser to the proper site (and kept it there).

        Since I've finally gotten better with Media Queries in CSS I don't use MD anymore and only have to create one website....

          Write a Reply...