XF 2.0 Callback is invalid (error_invalid_class)

HappyWorld

Well-known member
Hello.

When i tried to add code event listener i got this error :
Code:
Callback MyMobileDetection\Visitor\Visitor::getStructure is invalid (error_invalid_class).
What do you think is wrong?
  1. I have created add on using command line.
  2. I create php file in
    Code:
    \src\addons\MyMobileDetection\Visitor.php
  3. Listen to event : visitor_setup
  4. Event hint : empty
  5. Execute callback :
    Code:
    MyMobileDetection\Visitor\Visitor
    ::
    Code:
    getStructure
  6. Enable callback execution : unchecked
And here is my php code in "Visitor.php"
PHP:
<?php
namespace MyMobileDetection;

class Visitor extends XFCP_User
{
    public function isMobile()
    {
        return true;
    }

    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);

        $structure->getters['is_mobile'] = ['getter' => 'isMobile', 'cache' => true];

        return $structure;
    }
}
?>
 
MyMobileDetection\Visitor\Visitor -> MyMobileDetection\Visitor :)


Fillip
I got new error message in browser :
Code:
Oops! We ran into some problems. Please try again later. More error details may be in the browser console.

And here is my browser console :
Code:
PHP: <br />
<b>Fatal error</b>:  Class 'MyMobileDetection\XFCP_User' not found in <b>C:\xf\upload\src\addons\MyMobileDetection\Visitor.php</b> on line <b>6</b><br />
 
Change XFCP_User to XFCP_Visitor then try again :)


Fillip
Thank you for keep helping me, but i get similar error message :
Code:
PHP: <br />
<b>Fatal error</b>:  Class 'MyMobileDetection\XFCP_Visitor' not found in <b>C:\xf\upload\src\addons\MyMobileDetection\Visitor.php</b> on line <b>6</b><br />

Do you think maybe my namespace in PHP file is wrong?
Or should i use the "use" syntax in my PHP file?

This is my first time using "namespace" & "use" :D
 
Wait hold on, I'm misreading, I thought you were adding a class extension. When you're adding an event listener, don't extend at all.

class Visitor extends XFCP_Visitor -> class Visitor

You should be aware that creating an event listener will not allow you to modify the entity structure of the user entity.

To do that, you need to add a class extension for XF\Entity\User and create a file in \src\addons\MyMobileDetection\XF\Entity\User.php
PHP:
<?php
namespace MyMobileDetection\XF\Entity\User;

class User extends XFCP_User
{
    public function isMobile()
    {
        return true;
    }

    public static function getStructure(Structure $structure)
    {
        $structure = parent::getStructure($structure);

        $structure->getters['is_mobile'] = ['getter' => 'isMobile', 'cache' => true];

        return $structure;
    }
}


Fillip
 
To do that, you need to add a class extension for XF\Entity\User and create a file in \src\addons\MyMobileDetection\XF\Entity\User.php
Ok i have moved & renamed the file as instructed, and changed my PHP code into your PHP code, and i get similar error message :(

Code:
PHP: <br />
<b>Fatal error</b>:  Class 'MyMobileDetection\XF\Entity\User\XFCP_User' not found in <b>C:\xf\upload\src\addons\MyMobileDetection\XF\Entity\User.php</b> on line <b>4</b><br />

I even tried to do class extension but i get similar error :
Code:
Fatal error: Class 'MyMobileDetection\XF\Entity\User\XFCP_User' not found
 
Back
Top Bottom