Friday, March 29, 2024

Object Oriented Programming in PHP: Class 2 – Uses Of Objects

In our previous class, we explored the very basic principles of OOP. Today we are going to explore thee concepts with a little more clarity, and see precisely how we might use OOP to our advantage by creating code that is not only organized, but works well.

The use of OOP is not only an evolutionary step in a developer’s career, but a vital step in creating a software application that is scalable, maintainable and quality. The added bonus of having an object that contains everything related to that object within the encapsulation of the class really makes OOP a powerful and simple way to do things.

Let’s Build an OOP App

Let’s build a little application to prove the point. For our application, we’re going to build a class that will allow us to perform simple calculations like a calculator. We would need to add, subtract, multiply and divide two numbers. Let’s have a look at how we are going to do this.

In the screenshot below I have defined the class. Take a minute to read through that extremely simple code, then let’s start to dissect it and see how we could use this object.

As a small sidenote – this object is going to only use Methods (or functions) and not Properties at this time.

class

Let’s start with the details of the class before we actually put it to work.

Code Breakdown by Line

Line 3 defines the name of the class, which in this case is calculator. This means that when we instantiate the object for use, we would have to call $anything = new calculator();. You will notice that this object has no contructor method defined (public function __construct() {}). The reason for that is because we do not need to do anything on instantiation of the class, which is the only time that the contructor actually runs automatically.

Line 5 defines the Method ‘add’, which is a Public Method. You will recall from part one, that methods are simply functions within a class, and that Methods can either have a public, protected or private accessor defined with it. This will mean that if the accessor is Public, we can access it from anywhere outside of the class. If it is Protected, we can access it in any class that extends or is extended by the class, and if it is private we can access it only directly from within the class that it is defined in. Luckily for us, this Method is public, and can be accesses anywhere. We also see that two arguments are passed into the Method Add, namely $a and $b. This works exactly the same as a normal PHP Function, and in this case we are passing in two values that we would like to add together.

Line 7 is a simple check that we actually have both of the values ($a, $b) passed into the method. If not, it returns an error.

Line 12 returns the product of $a and $b.

The rest of the methods do pretty much the same, and the code really speaks for itself in terms of what they do. Let’s rather have a look at how we can use this very simple object.

We need to define the object first.

$calc = new calculator;

Notice that we did not have to add the parenthesis at the end of the class name as there is no argument being passed into the constructor.

Now, we can perform addition, like this:

$result = $calc->add(2, 5);

And then we can simply say:

echo $result; which will return 7.

We could also say: echo $calc->add(2, 5); for later use.

In the same way, we could use the other methods by saying things like:

echo $calc->divide($calc->add(4, 3), $calc->multiply(2, 5));

The result would boil down to echo $calc->divide(7, 10);

And therefore give us an answer of 0,7.

Now, let’s try something new. What I would like to introduce here is one more method and one Property, which will make the class much more programmatically sound as well as more powerful in the longrun – although with a class this simple it might not be that apparent. Please see the screenhot below.

class 2

Here we have the same object more or less, we’re just using it in a different way. I would argue this way is better, and that’s because we have removed a lot of repetition from the class methods, we have set the methods that do the work to only be accessible from within the class by making their accessors private, and we are no longer just returning a value in every method, but instead we are accessing the property ($result) from one method.

The usage if this class would not be a little different. For instance, to add two numbers together, we would now have to say – once the class has been instantiated as above:

$result = $calc->get_result(2, 5, ‘add’);

and to divide: $result = $calc->get_result(10, 5, ‘divide’);

If you haven’t caught on yet, the third argument we are passing into the get_result method is the name of the method we need (add/subtract/multiply/divide) and the $result property is set to “undefined” by default.

In fact, there is a way to access the $result Property directly, which is echo $calc->result; but can you think of another way to do this that would be much better? Feel free to discuss this in the comments section below.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured