Namespace problem with plugin functions

Hi there!

I’ve recently moved to sage for new projects and it works amazing! great job everyone!
Currently I’m rewriting an older project from Underscores to Sage and I can’t get the following to work.

On the website I use a few customised Tribe Events Calendar functions to get the amount of tickets left. I added a new config file /lib/tribe.php to define the function with the namespace Roots\Sage\Tribe. If anyone knows how to fix this, it’s probably a basic PHP thing I’m missing… Thanks!

function tribe_events_single_ticket_soldout( $event = null ) {
  if ( null === ( $event = tribe_tickets_parent_post( $event ) ) ) {
    return false;
  }

  $some_have_soldout = false;

  foreach ( Tribe__Tickets__Tickets::get_all_event_tickets( $event->ID ) as $ticket ) {

    if ( ! $some_have_soldout && 0 == $ticket->stock() ) {
      $some_have_soldout = true;
    }
  }
  return $some_have_soldout;
}
add_action('', __NAMESPACE__ . '\\tribe_events_single_ticket_soldout');

The function is called on several places but gives an error because of this line:

foreach ( Tribe__Tickets__Tickets::get_all_event_tickets( $event->ID ) as $ticket ) {}

Because the Tribe__Tickets__Tickets isn’t part of the namespace or something.

The full error:

Fatal error: Uncaught Error: Class 'Roots\Sage\Tribe\Tribe__Tickets__Tickets' not found in /Applications/AMPPS/www/jam/wp-content/themes/alloy-sage/lib/tribe.php:58 Stack trace: #0 /Applications/AMPPS/www/jam/wp-content/themes/alloy-sage/tribe-events/list/single-event.php(30): Roots\Sage\Tribe\tribe_events_single_ticket_surgeon_quant() #1 /Applications/AMPPS/www/jam/wp-content/plugins/the-events-calendar/src/functions/template-tags/general.php(131): include('/Applications/A...') #2 /Applications/AMPPS/www/jam/wp-content/themes/alloy-sage/tribe-events/list/loop.php(38): tribe_get_template_part('list/single', 'event') #3 /Applications/AMPPS/www/jam/wp-content/plugins/the-events-calendar/src/functions/template-tags/general.php(131): include('/Applications/A...') #4 /Applications/AMPPS/www/jam/wp-content/themes/alloy-sage/tribe-events/list/content.php(26): tribe_get_template_part('list/loop') #5 /Applications/AMPPS/www/jam/wp-content/plugins/the-events-calendar/src/functions/template-tags/general.php(131): include('/Applicati in /Applications/AMPPS/www/jam/wp-content/themes/alloy-sage/lib/tribe.php on line 58

A quick and dirty solution is just to not use a namespace for this particular set of functions. While it’s definitely a best practice to use a namespace, it’s causing you problems here.

1 Like

Ah great, I thought that would give some compile errors, but it did the trick.
It might be dirty, but it worked in the last version and it are just a few, very specific, functions, so it’s probably okay.

Thanks!

1 Like

That’s awesome. If you don’t mind (and agree), could you mark my reply as the solution to this thread? Thanks!

1 Like

FYI, in your code above you could have just changed Tribe__Tickets__Tickets to \ Tribe__Tickets__Tickets

3 Likes

Yeah, do what benword said. It’s a little confusing in PHP, but classes in a namespaced file need to either be imported with the use keyword or referenced fully, in this case since the Tribe classes are in the global namespace you reference it as \Tribe__Tickets__Tickets as Ben mentioned. Functions are automatically assumed to be in the global namespace unless you reference them as being namespaced. Slightly different and something you just have to kind of get used to.

Whenever you see an error like that: Fatal error: Uncaught Error: Class 'Roots\Sage\Tribe\Tribe__Tickets__Tickets' not found, just check, is the namespace correct on that class/function? If not, you probably need to fix it in your code.

4 Likes

I officially retract my above recommendation. I posted it without thoroughly reading your code. Do what Ben said.

1 Like

Ah thanks guys! Will change it! :slight_smile: