A player subclass for Sports Bench

Adding a custom player subclass for a specific sport

Sports Bench comes with five classes: players, teams, games, playoff series and playoff brackets. Because the plugin supports five sports, however, they’re written to be fairly generic.

But you have just one sport and it would be nice if there was a player class that was written to match your sport. The good news? There is a way in PHP to do just that with subclasses.

So admittedly, there won’t be a lot of details in this tutorial. Newbies to PHP will learn a bit about subclasses, but experienced programmers already know about subclasses, though this might spark some thought.

Either way, let’s create a player subclass for our site.

How to add a player subclass

So before we start adding subclasses, we need to decide where this subclass will reside. You could add it to your theme’s files and then use the include function your functions.php file to hook it up. But with something like this, it’s probably best to create a custom plugin to house custom subclasses.

So, in your plugins directory, add a new directory with the name of your plugin, like sports-bench-cusotmizations. Then in that directory, create a php file with the same name as the directory, like sports-bench-customizations.php. In that file, paste the following:

<?php
/*
Plugin Name: Sports Bench Customizations
Plugin URI:
Description: A place to put customizations for the Sports Bench plugin
Version:     1.0
Author:
Author URI:
License:     GPL3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
?>

Like the style.css file for themes, this is information for WordPress to use about your plugin. Next, go to the plugins page in you admin area of your site and activate this new plugin.

Next, create a file with the name of the subclass you’ll be creating. I’m going to be creating a soccer player, so my file will be sports-bench-soccer-player.php. Then, put the following line into the first plugin file we created so that the class file will be loaded.

include_once( plugin_dir_path( __FILE__ ) . 'sports-bench-soccer-player.php' );

Finally, to create the subclass, place the following lines into the class file.

class Sports_Bench_Soccer_Player extends Sports_Bench_Player {
}

The key here is the extends Sports_Bench_Player part. This makes our new class a subclass of the normal player class in the plugin. This means that your new subclass acquires the same attributes and functions, including the class constructor, as the parent player class. This makes it super easy to customize since we don’t have to write code twice.

Now let’s start to add some functionality to this new player subclass.

Adding functions to the subclass

Okay, so the subclass has been added, now let’s add some attributes and functions to this class. First, the attributes.

You can add attributes to a class like so:

$this->goals;
$this->assists;

These attributes don’t have any values yet, but you could write another __construct() function to fill them with values.

But let’s get to the really fun part: functions. This is where you can really make things easy for you. You can create functions that grab different stats so that you don’t have to write SQL over and over for simple things. And that’s just the tip of the iceberg.

To keep this tutorial simple, lets add two functions: a get_goals() function to grab the number of goals a player has and a get_stats() function that returns all of the stats for a player.

So the get_goals function will be simple. It will take an optional parameter, season, and then return the number of goals for that season. In the season parameter isn’t entered, let’s return the career goal stats.

So add the following function to the player class.

https://gist.github.com/ViewFromTheBox/5e8fe4d4607d494a2c55e6b1806c5ca5

That’s all we need for this one. You’ve created a function for the subclass. Now let’s add some more functionality.

Now for that second function, we’ll assume we’ve created other functions like get_goals so that we can return all of the stats for the player. Like the get_goals function, we’ll have an optional parameter, season, which has the same functionality.

So now add the following function to the subclass.

https://gist.github.com/ViewFromTheBox/f518f246eb033d6885142301fb3408b5

And that’s it. This function will return an array of stats for the player. Again, this is a brief tutorial to show you what you can do with subclasses and how it can help your site.

What does this mean for your site?

So what does this all mean for you? Well, it means that you can make customizing your site to fit you needs a whole lot easier. The reason the player and other classes were created in the first place was to minimize the need to write more SQL to get data from the database. Now you can do the same with your customizations.

What you do with this new information is up to you. Let your imagination run wild. And if you come up with a cool implementation of a player subclass, let us know. We’ll feature it on the site.

Happy developing!

[sports-bench-other-posts category_slug=”tutorial”]

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.