Object Oriented PHP tutorial-Detailed Guide

Spread the love

What is Object Oriented PHP?

Object Oriented PHP: Before looking into what is object oriented PHP, let’s see some background.

Object Oriented PHP

Look around you! Everything around you is an object. Even you yourself is an object.

Every object has a specific type. Like you, a male or a female is of type Human.
The chair you are sitting is of type furniture, the laptop or PC you are using is of type Computers.

Apple’s iPhone, Samsung’s Galaxy are of type Mobile phones. Eagles, Sparrows are of type Birds,  etc, etc.

Also, each object or a type of object is having certain properties. Like humans have two hands and two legs, birds are having feathers, computers are having memory, processor, keyboards. etc.

After properties, every object is also having its own functionalities. Humans can walk, talk, run, Birds can fly, computers can process. etc.

Similarly, in programming with object oriented features, every concept is treated as an object and of a particular type. That particular “type” in Object Oriented Programming PHP is called as a “Class”.

Using class we can create actual objects. We will see what is a concept called “Class” in PHP OOPs.

See the list of topics in object oriented PHP which you will learn in detail, in this tutorial series.

Learn in detail what is a Class, objects and their properties and methods in this chapter.

In this chapter, you will learn how to access the class properties and methods inside the class itself.

Learn about the two magic methods used to create a constructor and destructor in a class.

In this chapter, you will learn what is public and private access modifier and their use in a class.

This chapter will uncover in details about the inheritance between classes and its importance in code reuse.

OOPs feature an abstraction is explained in this chapter.

Learn the powerful and very useful concept of the interface which simplifies the code structure.

The most popular and widely used design pattern Dependency Injection is in detail in this chapter.

Polymorphism concept using an interface is explained in this chapter.

Learn about the Static properties and static methods in OOP PHP in details in this chapter.

A new feature in PHP 5.3 added named Late static binding which is helpful in code reuse is explained in this chapter in detail.

Another new feature which is added in PHP 5.4 is Traits. Learn in detail in this chapter.

Learn in detail about the Type hinting feature of PHP 5 and PHP 7 in detail.

Learn about Type Hinting in Interface and abstract class in this chapter.

The methods which get called automatically as per certain events called as magic methods explained in this chapter.

There are magic constant also present in PHP. Learn about them in this chapter.

The function which does not have any name. learn how to use them using this chapter.

What is a Class?

Before looking into the answer lets first see how a class looks in PHP OOps.

class Mobile
{

}

As you see in above code, we have defined a class of TYPE Mobile. We have written a class using three things – class, Mobile, and curly braces {}.

Here

“class” – is a keyword in PHP more specifically in an Object oriented programming PHP. This keyword in a code tells that we are creating a class.

“Mobile” – is the user-defined name of a class. This is the name of a class (or a TYPE) of objects. As you see we have written “M” in Mobile in the capital letter. It is a convention that we should write the name of a class with a first letter capital.

“{ }” – curly braces are simply a block where we will write code.


So in order to write a class, you need to use access specifier – a keyword class, name of a class and curly braces.

– As we have seen different real-world examples in the above description, here “Mobile” is a type of object. And every type or an object is having properties and functionalities.

– These properties and functionalities are defined inside a class in curly braces. So the class is just a combination of properties and functionalities.

– A class is just a structure, blueprint, template of an object. The class will tell how an actual object will look like, which properties and functionalities will an object have.

Imagine how Bricks are made

Object Oriented PHP
Imagine how bricks which are used to build, buildings walls are made.

To make a brick first clay is poured inside a brick case, or brick maker, which is nothing but a framework or a template which converts clay into a brick.

That brick template is having some properties, like length, breadth, height, area, etc, which will be applied to a lump of clay which will put inside it. After clay gets dry, you will get a brick of the same length, breadth, and height.

So, in this case, that brick case is nothing but a class using which actual objects i.e. bricks will be made.

I think you have got a clear picture now about what actually a class is in an object oriented programming PHP.

How to add properties in a class in Object Oriented PHP?

What will be the properties of a mobile phone?

Properties of mobile would be a model name, like the iPhone, a color, size, amount of RAM, memory space, camera, etc.

And functionalities would be calling, playing videos, taking pictures, etc.

So now we have to add these properties and functionalities to our class Mobile.

Adding properties:

public class Mobile
{
    public $companyName;
    public $color = "Red";
    public $hasCamera= true;
}
As you see in the above example we have added few properties inside a class Mobile i.e. model_name, color and has_camera.

These properties are declared with –

public keyword – “public” is an access specifier. Will look into access specifier in detail.

Properties variables with $ sign – $companyName, $color, $hasCamera.

We have assigned default values for few properties like a string value “Red is assigned to $color and a boolean value “true is assigned to $hasCamera. And a property with no default value $companyName.

You can add properties of any data type and assign a value inside a class.

Properties names start with small case letter. And if the name contains two words then you have to make a second word’s letter capital and combine both the words like – $companyName and $hasCamera.



Adding functionalities to a class: A member function

To add functionality to any class in object oriented PHP we just need to add a function to a class.

These functions are called as methods of a class.

Let’s see code below to add a method inside a class,

class Mobile
{
    public $companyName;
    public $color = "Red";
    public $hasCamera= true;

    public function calling()
    {
        return "Calling from a mobile.";
    }

}

As you see in the above example we have simply added a function named calling to the class, and that function will be called as a method named calling, of a class Mobile.

The syntax of a method in PHP OOPs:

The first part of the method is the “public” keyword, which is an access specifier to define the access level of a method. More on this later.<link>

The name of the method always starts with a lower case letter.

If more than one word used in the method name then we have to make the second word’s first letter capital and combine both the words.

This method calling() is a functionality of a class Mobile which is currently doing only one operation i.e. returning a string value “Calling from a mobile“.

We can add many other methods into a class as per requirements.

Now let’s see how to create actual objects from a class.

What are objects? How to create an object of a class?

In a class, the properties and methods are encapsulated and they are not accessible outside a class.

It means properties and methods are not having scope outside a class or you can say that they are not having global scope, but they are having a class scope.

As the class is nothing but just a structure, you need to make actual objects from that structure, an instance of that structure which can be used.

So an object is just an instance of a class, which is able to access properties and methods of a class, outside i.e in a global scope.

You can create an object of a class using a keyword – new.

$iphone = new Mobile();

 We have created the object $iphone on a class Mobile using a new keyword.

Creating an object is just using a class name with or without circular brackets, using a “new” keyword before the name and assigning to a PHP variable.

This process of creating an object from a class is also called as an instantiation as we are creating an instance of a class which is nothing but an object.

You can create as many objects as you required. Let’s create a few more objects.

$galaxy = new Mobile();

$note = new Mobile();

After creating an object all the properties and methods of a class are automatically gets included in an object.

Now you can access or get the properties of a class Mobile from any of its objects. For more details check out the official documentation.

How to get the properties of an object?

You can get the properties of an object using an arrow operator ->.

echo $iphone->companyName;

echo $iphone->color;

echo $iphone->hasCamera;

Here you can get properties by just writing an arrow sign after object name and then a property name.

The output of the above program will be:

a blank value, as companyName is not assigned any default value,

Red

1

One thing to note is that we have not used a dollar $ sign before a property name in echo $iphone->companyName. This is the convention in PHP OOps. You do not need to add a $ sign in properties name while accessing them.

This will print the default values of properties assigned to them inside a class.

How to set properties of an object in object oriented PHP?

In object oriented programming PHP, you can set or change the values of properties as per the objects.

Like $iphone can have a value of a property companyName as “Apple“.

$galaxy object can have companyName as “Samsung“.

Let’s assign the properties to the objects

$iphone->companyName = 'Apple';

$iphone->color = 'Black';

$galaxy->companyName = 'Samsung';

$galaxy->color = 'Gold';

Here we have just assigned the values of different properties directly to the objects, using assignment operator “=”.

Here properties are just member variables which can be accessed using object name and an arrow sign.

How to access methods of a class?

Same like accessing properties of a class using objects you can access a method of a class also, using method name with circular braces, like,

echo $iphone->calling();

echo $galaxy->calling();

As we are having a method “calling” declared inside a class Mobile you can access the method’s functionality by calling that method using an object with an arrow operator and a method name.

The output of the above statements will be the same as both the objects have the same method defined for them i.e. calling(). –


Output – 

Calling from a mobile.

Calling from a mobile.

So this was the first chapter on the basics of a class, object properties, and methods in object oriented PHP.

In the next chapter, we will see how can we access properties inside a method and how to pass parameters to the methods and assigning them to the properties using $this keyword in detail.

Let’s see some of the advantages of Object Oriented PHP

  1. Code reuse through the inheritance concept – You can use the already defined code in parent class into the child class by the concept of inheritance. This makes you able to use already written code anywhere you want.
  2. Easy debugging due to modularity.
  3. Coding flexibility using polymorphism
  4. Easy testing.
  5. Fast development.

Stay tuned in this Object Oriented PHP tutorial series and learn in detail in the next tutorials.

Also feel free to comment and give your feedback about the tutorial, whether you like it or not and suggest the improvement if required.

Thanks!


Spread the love

Leave a Comment