Roots Discourse

How to make Class, added by Composer package, available in setup.php?

Hi guys,

I’m looking to implement Segment, an analytics tool. I’m using the install guide.

There a 3 basic steps:

  1. Add https://github.com/segmentio/analytics-php
  2. Add the file - require_once("/path/to/analytics-php/lib/Segment.php")
  3. Initialise Class - Segment::init(“KEY”)

I am installing via Composer (composer require segmentio/analytics-php), but am not sure how to implement the Class. Currently I am receiving ‘Class ‘App\Segment’ not found’.

The question: How do I make the Class (Segment) available in my setup file? Is it auto loaded when added to the composer file?

Thanks!

You aren’t initializing the class with the correct namespace. If you look at the source files for that package, the class you’re trying to initialize has no namespace. When you try and initialize it as above PHP attempts to concatenate the App namespace, and that doesn’t work. You need to prefix the class with a \ to call it from the root namespace, i.e. \Segment::init('KEY').

For a little more on namespacing, etc: https://roots.io/namespacing-and-autoloading/

1 Like

@alwaysblank That was it! thanks for the quick help :smiley: