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
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:
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
As I mentioned in the comments under the Question, updating the namespace to:
should work.
Another option (as mentioned in the README of the project) is to load the class as:
which should correctly load the class.