OOP Fundamentals For WordPress

By: Ryan Kienstra on: January 11, 2015  in: OOP For WordPress

OOP FundamentalsYou program for WordPress using PHP functions and variables.

But you’d like to use object-oriented programming.

Maybe you’ve read my guide, OOP With WordPress: Why?

In writing functions, you’ve already used:

OOP Fundamentals

  • Abstraction
  • Code Reuse

Abstraction

Functions often have private variables.

They can be abstracted from the local scope.

Nothing else can access them, so you can be confident of the results.

function get_sum( $numbers ) {
        $accumulated_sum = 0; // private variable
        foreach( $numbers as $number ) { 
                $accumulated_sum += $number;
        }
        return $accumulated_sum;
}

$accumulated_sum is only accessible inside the function.

But it’s destroyed after the function returns.

What if you want call the function many times, and keep the sum for all of them?

You should…

Create A Class

Classes are the main unit of object-oriented programming.

They have local variables called “properties”.

They’re abstracted. Nothing outside of the class can access them.

And properties aren’t destroyed.

class Collection_Math {
        protected $accumulated_sum;

Classes also have functions, called “methods.”

So we’ll move get_sum() into the class.

class Accumulator {
        protected $accumulated_sum;

        function get_sum( $numbers ) {
                foreach( $numbers as $number ) { 
                        $this->accumulated_sum += $number;
                }
                return $this->accumulated_sum;
        }
}

Code Reuse

Functions can be called anywhere they’re useful.

You can use get_sum with any array of numbers.

Reuse of code with functions reduces complexity.

Whenever you call get_sum, you don’t have to think about the code inside it.

But classes are more flexible than functions.

So OOP allows much more code reuse.

Using A Class

You’ll first create an instance of the class.

This is also called an object, and it’s like a version of the class.

It’ll reuse all of the class methods.

But the properties of this instance can be different from those of the class.

Or other instances.

We’ll create an instance:

$my_accumulator = new Accumulator();

This calls the __construct() function below.

class Accumulator {
        protected $accumulated_sum;

        function __construct() {
                $this->accumulated_sum = 0;
        }

        function get_sum( $numbers ) {
                foreach( $numbers as $number ) { 
                        $this->accumulated_sum += $number;
                }
                return $this->accumulated_sum;
        }
}

The function __construct() sets an instance property:

$this->accumulated_sum = 0;

By using $this-> in a class, you access the instance properties.

We’ve created an instance with $my_accumulator = new Accumulator();

Now, let’s use it to get a sum.

$first_sum = $my_accumulator->get_sum( array( '7' , '9' ) );
var_dump( $first_sum );

This will print int(16).

$my_accumulator will retain the value of 16, so we can keep adding numbers to it.

$second_sum = $my_accumulator->get_sum( array( '6' , '8' ) );
var_dump( $second_sum );

This will print int(30).

A Different Instance

You can reuse classes by creating different instances.

Their properties can be different.

$different_accumulator = new Accumulator();
$different_sum = $different_accumulator->get_sum( array( '3' , '6' ) );
var_dump( $different_sum );

This will print int(9).

Familiar Ground

By writing functions, you’ve already used OOP Fundamentals:

  • Abstraction
  • Code Reuse

But classes apply these principles more thoroughly.

These are only the first examples of code reuse.

Next, you’ll see how classes are even more flexible.

And even more powerful to reuse.

  • This field is for validation purposes and should be left unchanged.

Leave a comment

Get Free Updates

  • This field is for validation purposes and should be left unchanged.