serbanghita / Mobile-Detect

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
http://mobiledetect.net
MIT License
10.54k stars 2.68k forks source link

Uncaught Error: Class "MobileDetect" not found #916

Closed Lavrynenko closed 1 year ago

Lavrynenko commented 1 year ago

There is the file test.php: <?php require_once 'MobileDetect.php'; $detect = new MobileDetect; echo "$detect"; ?>

File "MobileDetect.php" located in the same folder. But when I run the code, I get an error: Fatal error: Uncaught Error: Class 'MobileDetect' not found in C:\xampp\htdocs\Php\telegramid.net\test.php:3 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Php\telegramid.net\test.php on line 3

File "MobileDetect.php" downloaded from https://github.com/serbanghita/Mobile-Detect/archive/refs/tags/3.74.0.zip

PHP version: 7.4.33

please tell me what is wrong? Thank you!

dugwood commented 1 year ago

As stated here: https://github.com/serbanghita/Mobile-Detect/releases/tag/3.74.0

library exported as Detection\MobileDetect

I think that's your issue. Just add:

use Detection\MobileDetect;

At the beginning of your script.

Lavrynenko commented 1 year ago

use Detection\MobileDetect;

I try the code: <?php use Detection\MobileDetect; require_once 'MobileDetect.php'; $detect = new Mobile_Detect; echo "$detect"; ?>

And I get an error :( I'm doing something wrong, but what? :(

dugwood commented 1 year ago

What you're doing wrong is that you don't provide the test.php source code :-)

Lavrynenko commented 1 year ago

Что вы делаете неправильно, так это то, что вы не предоставляете test.phpисходный код :-)

Grabbed my head :(

The code: use Detection\MobileDetect; require_once 'MobileDetect.php'; $detect = new Mobile_Detect; echo "$detect";

dugwood commented 1 year ago

You must have another issue (that's not the first one you've opened this issue for).

OK, I've spotted it, you still use Mobile_Detect instead of MobileDetect.

Try:

<?php
use Detection\MobileDetect;
require_once 'MobileDetect.php';
$detect = new MobileDetect();
var_dump($detect->isMobile());
Lavrynenko commented 1 year ago

You must have another issue (that's not the first one you've opened this issue for).

OK, I've spotted it, you still use Mobile_Detect instead of MobileDetect.

Try:

<?php
use Detection\MobileDetect;
require_once 'MobileDetect.php';
$detect = new MobileDetect();
var_dump($detect->isMobile());

Thank you!! Everything worked - tested on a mobile device and a computer. If from a mobile device - true, otherwise - false :)

Thank you very much for your help!