skip to Main Content

I am migrating an older application from PHP 7.0 to 8.1 on an ubuntu system. The app is using a library called MobileDetect. The readme of the latest verion of the library which I migrated to, states compatability only for PHP >=7.3,<8.0

As it is a lightweight library I was still hoping to be able to use it with php 8.1, however even the class call is failing:

Executing file:

require_once '../app_global/Mobile_Detect.php';
$detect = new Mobile_Detect;

File Mobile_Detect.php:

<?php
namespace Detection;
use BadMethodCallException;
class Mobile_Detect
{
...

PHP error log:

PHP Fatal error:  Uncaught Error: Class "Mobile_Detect" not found in 
/home/www/project/qry_index.inc:11nStack trace...

The name space seems right, there is no capital letter misspelling, so why is the class not found?

2

Answers


  1. It’s possible that the Autoloader for the Mobile_Detect class is not properly configured for PHP 8.1. In PHP 8.1, the Autoloader behavior was updated to require the use of the ::class constant instead of a string class name in some situations.

    To fix this issue, you can try updating the require statement to use the fully-qualified namespace and the ::class constant instead of the string class name, like this:

    require_once '../app_global/Detection/Mobile_Detect.php';
    $detect = new DetectionMobile_Detect;
    

    If this does not work, you may need to update the Mobile_Detect library to a version that is compatible with PHP 8.1 or consider using an alternative library that is compatible

    Login or Signup to reply.
  2. As I mentioned in the comments under the Question, updating the namespace to:

    namespace DetectionMobileDetect;
    

    should work.

    Another option (as mentioned in the README of the project) is to load the class as:

    $detect = new DetectionMobile_Detect;
    

    which should correctly load the class.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search