Blog

Updating the Team Class/Object for Sports Bench 2.0

So as a part of the run up to the release of Sports Bench 2.0, I’m going to go over what’s different with the various different sections of the plugin and the main theme.

And in this post I’m going to start with the Team class. This class and the player class are probably the most used classes within the plugin itself and if you’re customizing it in any way.

As you’ll see, there are some pretty dramatic changes to the Team class, especially in how you are able to get and set the properties for the object.

So let’s discuss what will be different in the Team class in the version 2.0 release.

What’s new with the team class?

The biggest change for the team class is that the properties are now protected. That means that they can only be directly accessed through the class or a child class, and can’t be called externally.

So to help you continue to access them, I’ve created getter methods that you can use instead to get the properties. That way you won’t accidentally update a property instead of getting a property.

  • get_team_id
  • get_team_location
  • get_team_nickname
  • get_team_abbreviation
  • get_team_status
  • get_team_location_line_one
  • get_team_location_line_two
  • get_team_city
  • get_team_state
  • get_team_location_country
  • get_team_location_zip_code
  • get_team_stadium
  • get_team_stadium_capacity
  • get_team_head_coach
  • get_team_logo_url
  • get_team_photo_url
  • get_team_division
  • get_team_primary_color
  • get_team_secondary_color
  • get_team_slug

On the flip side, you can easily update a team by using the update method. This method accepts an array of key => value pairs that are “property” and then the value. It will then update the property for the object as well as update the team in the database as well.

All of the old functions are still available for you to use. Plus there are a few new ones you can use as well. You can check out the documentation for that shortly.

Also, please note that there is technically a change in how you call the player class. Before it was Sports_Bench_Team. Now it is just Team, but you need to place use Sports_Bench\Classes\Base\Team at the top of your PHP file to call it.

Sport-specific team classes

Also new with Sports Bench 2.0 is the introduction of team-specific classes that you can use.

At this moment, they don’t contain any new properties from the parent Team class. But they do each have a different definition for getting things like team stats and individual team player stats. And I might add in new methods and properties for each sport in the future.

So I would recommend checking out the team class specific to the sport that you use and using that, especially if I add in more methods and properties specific to that sport.

And creating a team object from the sport-specific class is super easy. You simply use “new {sport}Team”. So if your website is for a soccer league, you would use new SoccerTeam( (int) $team_id ) to create the method.

One quirk to know about

Finally, I do want to go over one quirk in trying to create a new team object, whether it’s the general one or a sport-specific one.

You can create a team object with either a slug or the team id. The reason for this really stems from how the individual team pages are created (which uses the slug and not the id).

So in order to create a team object using a team id, you’ll need to place (int) before the team id when constructing the object.

<?php $team = new Team( (int) 1 ); ?>

This actually isn’t something that’s new, but I’m not sure I’ve ever discussed it before. Just something to be aware of.