CODING ACADEMY

View Original

What is PHP OOP? Object-Oriented Programming for Beginners

I often hear Object-Oriented Programming (OOP) referred to as a programming style that uses ‘blueprints’ (hence the use of the house for this blog’s graphic!). In many ways, I can see why that is the case.

WHAT IS THE DIFFERENCE BETWEEN PROCEDURAL AND OBJECT-ORIENTED PROGRAMMING

Many reading this blog will, no doubt, come from a procedural programming background. Procedural coding deals with functions that manipulate the data. Object-oriented programming (OOP) has objects that contain the data and functions (methods).

If you are dealing more with function, then procedural programming is the way to go. If you know you will be dealing with data then OOP is probably the route you should take.

If you know that you will be extending your code over time then OOP can be easier to update and manage.

I would argue that, for some, there is little advantage in learning OOP. Most hobby coders write fairly limited scripts where procedural code will often be much easier to follow.

See this content in the original post

IS PROCEDURAL PROGRAMMING EASIER TO LEARN?

The linear nature of procedural programming means that is can be easier to learn. However, this also means that, by that very linear nature, code can often be repeated.

The linear nature also means that it can be easier to check the code. Some may find it more intuitive when first learning.

Some older readers may recall the days of BASIC. Coding was one line after the other, simple, clean, and very easy to learn.

I mentioned above about the possible repetitive nature of procedural coding. Code cannot be easily reused.

Those of you who have followed my PHP for Beginners courses will know how easy it can be to learn this procedural style of coding.

WHAT IS OOP?

Basically, object-oriented programming (OOP) revolves around using ‘objects’ - those blueprints that I mentioned earlier. The blueprints are known as the ‘class’.

So, the architect has just handed you a very large rolled up piece of paper. You unroll it to reveal the plans for your new house. You can take these plans and build your house.

You could then pass on the plans to friends and they could also build a house from the same plans.

Each of you has build an ‘instance’ of that house.

However, when building their house, your friends decide to have a red door instead of the blue door that you have. In fact, there are many ‘properties’ about the houses that could be different.

The action of changing the colour of the door is the ‘method’.

Each house is an ‘object’

The language of OOP is a little different than procedural coding. Above, I have made reference to:

  • classes

  • instances

  • properties

  • methods

  • objects

Can you think how the above can be applied when ordering a new car?

You drop by the Ford dealership and like the look of the latest Ford Mustang. You decide to place an order.

The basic structure of the car is the same. When the factory starts building your car they are creating an ‘instance’ from their blueprints (the class).

When you placed your order you could decide on the ‘properties’ of the car e.g. colour of the car paint, the interior, etc.

The car that rolls off the assembly line is the ‘object’.

After 2 weeks you decide that you no longer like the colour of your car. You take it back to the dealer and ask them to change the colour.

In OOP speak, you are taking the object and using a method to change the property.

HOW TO DEFINE A CLASS IN PHP?

Let’s use the car example from above.

See this content in the original post

We declare a PHP class by using the keyword ‘class’ and then give our class a name. Note, Pascal case is used for naming classes. This is the practice of capitalising the first and subsequent words.

Currently, the class is defined but it does not have any properties or methods.

Let’s add the price and colour properties to the car:

See this content in the original post

Lines 6 and 7 declare the price and colour variables.

You may notice the use of the keyword ‘public’.

Class properties are defined as either: public, private, or protected.

In our example case, the colour and price need to be known to the buyer and we need to be able to change these.

If we had another variable for dealer profit margin then this would be set to private or protected and this would only be available internally.

Note that we have not yet set a value for our properties. This would be done once we place our order.

So, let’s go ahead and start an order to build the car:

See this content in the original post

Line 12: we declare a variable called $myCar and then create a new instance of the Car class.

Note that this is outside the Car class (lines 3 to 9).

We should decide how much we are willing to pay for the car.

See this content in the original post

Before I explain this code, let’s think about how it will be executed.

Lines 3 to 13 are just declaring the class but are not used yet.

We get down to line 16 and create a new instance of the Car class. We now have an object called $myCar.

Line 18 now operates on the $myCar object. It wants to set the price of the car to 50000. We are passing in the argument 50000 to the method called setPrice.

Lines 10 to 12: we create a method that will set the price of the car.

Line 10 simply gives the method a name - in this case we call the method setPrice. We are passing in the $myPrice parameter.

Line 11: $this refers to the PHP object that was created. Take the object and set the $price variable to the 50000 that was passed in. Note that we do not include the $ in front of the variable name $price.

This can be a little confusing for beginners so take time to study what is going on here.

SETTING THE COLOUR

Now that we have set the price you should have an idea of how to set the colour.

See this content in the original post

Although the separate methods can be used for setting the price and colour, we can used a constructor method as follows:

See this content in the original post

Lines 10 to 13 set up the constructor. Note the use of the double underline character before the construct name.

Line 18: when creating an instance of the Car object we pass in the price and colour as arguments. As soon as the object is created the constructor is called and the price and colour are used.

OUTPUT THE PRICE AND COLOUR

We can output the price and colour using some more methods as follows:

See this content in the original post

Lines 16 to 22: we have added 2 new methods which retrieve the price and colour.

Line 29: get the price property of the $myCar object, assign it to the variable $myPrice, and echo the result.

Line 30: get the colour property of the $myCar object, assign it to the variable $myColour, and echo the result.

SETTING DEFAULT VALUES

As we all know, the default colour of many new cars is white and cars also have a default retail price.

We can set a default price and colour as follows:

See this content in the original post

Line 10: the default price is set to 60000 and the default colour to “White”.

Line 27: we just create a new object and do not have to pass in any arguments.

If we wanted to override the default values then we could simply pass in arguments as we did previously.

CODING ACADEMY MEMBERSHIP

Coding Academy members have access to ALL video courses and materials.

See this content in the original post