0

I installed PostHog in my PHP codebase, and trying to use it by following the tutorial in this link (https://posthog.com/docs/integrate/server/php).

I am attempting to call PostHog::init function as a following:

       PostHog::init($apikey,
        array('host' => $baseUrl,
            "debug" => true)
    );

But I am getting error in this line PostHog::init, the error says

"Attempted to load class PostHog" from the global namespace. Did you forget a "use" statement?"

In fact, I am already using the "use" as following "use PostHog\PostHog;", but I am still getting this error. I can confrim that the Posthog library is intall becuase I can read the classess in Poshog library from my codebase.

This is more info about my app:

  • I use Symfony framework 5 and the app is deployed in docker. I use PHP 7.4 and posthog/posthog-php": "2.1.1".

  • I checked the vendor folder and PostHog is there (see photo attached) here

  • I implemented service call PosthogHandler where I use PostHog functions (like init, capture and etc). I am calling functions' services from controller. But the issue is that the error appear in the PosthogHandler constructor in line PostHog::init, at PostHog initialisation stage. This is my PosthogHandler service class:

<?php
    declare(strict_types=1);
    
    namespace App\Posthogs;
    
    use PostHog\PostHog;
    use App\User\User;
    
    class PosthogHandler
    {
        public function __construct($env, string $key, string $baseUrl)
        {
    
            PostHog::init($key,
                array('host' => $baseUrl,
                    "debug" => true)
            );
    
        }
    
        public function addEvent(string $eventName, User $user){
    
            PostHog::capture(array(
                'distinctId' => $user->getId()->id(),
                'event' => $eventName
            ));
        }
    
    }

Any help, why I am getting above error?

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • So what file are you trying call PostHog::init in? I'm sort of guessing it is in `public/index.php`? Which can be a bit problematical. – Cerad Dec 14 '22 at 13:56
  • @Cerad I use Symfony and my app is deployed in docker. So, I am just installing and trying to use it in the backend as testing first. I am calling PostHog::init from the backend, one of my service class. I have done a lot of third-party app integration before and I use the ssame way to use the 3ht party libraries , but I don't know why I can't integrate Posthog to my app? –  Ibrahim EL-Sanosi Dec 14 '22 at 14:12
  • Okay. Posting the handler code is progress. Is the handler being injected into something or are you using a `new PosthogHandler();` some place and if so where? – Cerad Dec 15 '22 at 14:10
  • @Cerad Yes, I use Sumfony autowiring: I configure the the initiation of the services in services.yaml file, for example ( App\\Posthogs\PosthogHandler: arguments: $key: '%env(POSTHOG_API_KEY)%' $baseUrl: '%env(POSTHOG_BASE_URL)%') –  Ibrahim EL-Sanosi Dec 15 '22 at 15:23
  • @Cerad I don't use a new PosthogHandler(). I injected the PosthogHandler in controller class where I am calling PosthogHandler functions –  Ibrahim EL-Sanosi Dec 15 '22 at 15:54
  • For what it is worth I added your handler class to a Symfony project and injected it into a controller action method. It all worked fine as expected. I suspect you still have a line of PostHog code someplace unexpected in your project. – Cerad Dec 15 '22 at 20:44
  • @Cerad I see, thank you for testing this. It is strange what is happening for me! I also when I press ctrl+cliick on class, it open POstHog class from the lib. Have you used version "posthog/posthog-php": "2.1.1"? –  Ibrahim EL-Sanosi Dec 16 '22 at 09:22
  • I installed the latest which composer says is 3.0.0. The PostHog::VERSION is still set at 2.1.0. I loaded 2.1.1 and took a look.he PostHog class itself seems to be about the same. Namespace and whatever looks fine. Sometimes I just start over with an empty project and see if I can replicate whatever problem I'm facing. – Cerad Dec 16 '22 at 13:59
  • But going back to PHP basics, the only way I know of to get your particular error message is to have a PostHog::init in a file without a namespace line and without a use PostHog\PostHog line. I did notice that you used `PosthogHandler` instead of `PostHogHandler`. Should not matter. I used your code unchanged. But maybe you have a case mismatch someplace. – Cerad Dec 16 '22 at 14:02
  • @Cerad I build non-symfony project and PostHog works for me without any issues. But it does not seem to work on the my existing app! I have not spotted why yet, really strange! –  Ibrahim EL-Sanosi Dec 16 '22 at 14:04
  • @Cerad The name PosthogHandler class is correct and this is how I created and declared in other classes. Do you mean trying this \PostHog\PostHog::init. If so, I have done this and getting the same error! –  Ibrahim EL-Sanosi Dec 16 '22 at 14:11

2 Answers2

0

The error indicates that your class file could not be autoloaded properly. You might want to check first that PostHog is present in vendor/autoload.php by creating a test file to check

<?php
require_once 'vendor/autoload.php';
var_dump(class_exists('Posthog\Posthog'));

If not you should try this command

composer dump-autoload
  • The error does not indicate an autoload issue. It's a lack of a `use` statement resulting in an incorrect fully qualified class name. I know the OP said they had one but their reluctance to provide actual details is making it challenging to help them. Symfony has a somewhat strange index.php and I am pretty sure the problem is with the way the OP is trying to use it. But we will probably never know. – Cerad Dec 15 '22 at 02:31
  • @Cerad I have added more details to issue, you can find it in the post above. And please let me know if there is any missing info. –  Ibrahim EL-Sanosi Dec 15 '22 at 09:14
  • @Beeksi Waais I called the class_exists function as you suggest, it returns bool(false), however, when I check the vendor folder I found Posthog folder inside it. I ran dump-autoload, then it doesn't solve the issue. –  Ibrahim EL-Sanosi Dec 15 '22 at 10:08
  • @IbrahimEL-Sanosi did you solve your problem? Because `class_exists()` returns false, it means that `vendor/composer/autoload_psr4.php` do not contain `Posthog\Posthog`. You can check but I don't think you will find it. It means you have a problem with your composer.json file. – Beeksi Waais Dec 16 '22 at 11:28
  • @beeksi-waais I have not solved the issue yet! and I checked the file you mentioned (vendor/composer/autoload_psr4.php) and I have got this line 'PostHog\\' => array($vendorDir . '/posthog/posthog-php/lib'), and this location is existed inside Posthog folder in vendor. –  Ibrahim EL-Sanosi Dec 16 '22 at 11:58
0

It works!

I have three vendor folders in (1) Frontend (1) Controller (3) Services.

Before, I only installed the PostHog library in services folder where I use PostHog functions. This didn't seem working despite the posthog lib appeared in vendor folder.

What it makes work and the error disappears when I installed the posthog lib in controller folder. I don't use posthog in controller at all, I am not sure why I need to install the posthog lib in controller folder in order to work. But eventually it works!