Updating the Player 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 Player 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 Player 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 Player class in the version 2.0 release.

What’s new with the player class?

The biggest change for the player 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_player_id
  • get_player_first_name
  • get_player_last_name
  • get_player_photo_url
  • get_player_position
  • get_player_home_city
  • get_player_home_state
  • get_player_birth_day
  • get_team_id
  • get_player_weight
  • get_player_height
  • get_player_slug
  • get_player_bio

On the flip side, you can easily update a player 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 player 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_Player. Now it is just Player, but you need to place use Sports_Bench\Classes\Base\Player at the top of your PHP file to call it.

Sport-specific player classes

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

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

So I would recommend checking out the player 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}Player”. So if your website is for a soccer league, you would use new SoccerPlayer( (int) $player_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 player object, whether it’s the general one or a sport-specific one.

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

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

<?php $player = new Player( (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.

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy

This site uses Akismet to reduce spam. Learn how your comment data is processed.