Object-Oriented Programming with PHP 7 

Object-Oriented Programming with PHP. Hello, All The Reader welcome in the PHP world. In this article, we will take over the all-new concept of PHP object-oriented programming. Well in this article we cover all the concept behind object-oriented programming in PHP. Object-oriented programming introduced in PHP 7 and later version but it is still available in version 5.6. In subsequent articles, we also talk about some major changes in PHP latest version and also take a look at all these new concepts with the example.

What you will learn in this article.

  1. what is object-oriented programming?
  2. how to create a class with properties and methods?
  3. what is the use of object-oriented programming in PHP?
  4. how to create a class with properties and methods?
  5. what is the used of properties and method in the class?
  6. how to make an instance of the class?
  7. how to access all the properties and methods of a class using the object?

so in this article, we cover the most basic concept of object-oriented programming.  We will start looking first what is object-oriented programming, Then we take a look What is the use of object-oriented programming in PHP, how you can create a class with properties and methods.  Where and how you can use the properties and method in the class how to create an instance of the class and how to access all the properties and methods of the class using the object.


Understand Object-Oriented Programming

Object-oriented programming ( OOP )  it’s all about objects. As given in the word OOP  is relate to objects. so we first want to understand what is an object. The object is just data group together to use in the application as a single entity.

You can take object just like an array.  The array can store multiple data with key and pairs.  but object store different data with properties and methods.  you can take property as a variable of class and methods as function. You can take an object as a single entity that store multiple different values. For example, if you wanted to track the person then you need to create an object of a person with the properties name address phone number gender etc.

Now we cover what is the object all about takes a look at how to create objects using class.


How to Create Object

What is object-oriented programming with PHP mean? The object is just an instance of the class. It means to create an object you need to create a class.  now to create a class When you to follow the structure of the class to create it.  the class structure contains properties and methods. when you create a class you always specify properties on the top just after the class name and methods are declared just after that.  properties and methods are the optional part of your class. if you don’t want to specify a property to your class you can do that with methods also. but it will not make sense to create a class.

now take a look at the example that demonstrates to you how you can create a simple class in your PHP program.

Description of the code

This is the great example of the class.  in this example, we created a class using class keyword and specify a name for the class so we choose Test. After that, we specify some properties with public-private and protected keywords.  we’ll talk about this keyword in subsequent articles. But for basic understanding, public members can be accessed in the class and outside of the class, Protected members can be accessed in the class and in other classes which included in the same package and private member can only be accessible within the class.

Then we created three methods.  to create methods we first want to define the accessibility, then we specify function keyword and specify the name of your function. There is a different type of functions you can create in the class we will talk about later in the future article. We will take a brief description of functions and will give you and complete understanding of how you can create the different type of functions in your class.

so the first function is getName().  this function will return the string value. Using this type of function you can return any value like integer Boolean string float an object.  Keep in mind that object is also and data type you can use it anywhere you want. but when you specify one object value to another it must have the same data type.


What is the use of Properties and functions

So let’s talk about what is the use of properties and methods in the class.  The properties basically act like a variable that store data and methods actually a function that accomplishes your task.  now if you ponder where to use function parameter and where to use return value?

The answer is very simple.  if you wanted to get some value from the user you can pass the parameter to the function it might be one or more than that.  a function that Returns a value is used to perform some task and return manipulated value to the user.

in the above example, we had inserted the third function that calculates two values and returns it.  now if you specify some values in the parameter as an argument then the function return calculation of these two values. 

Then you can store this value in any variable you can manipulate that you can make any changes in that value after the function returns it. So basically the function created only to accomplished tasks.

what about the properties?  Now class properties use to store the values of the class. Suppose if you wanted to create a variable that multiplies two values but we don’t want to return it.  But after sometimes if you wanted to access that value in you need to perform the function again. In that situation, we specify a code in the function that store the function return value in the class property. So then you can access this properties value anytime if it is not changed.

I hope you understand the basic use of properties and methods of classes.


Where to use Class and How?

Now, this topic basically gives you an understanding that how you can use these functions in your program.  as I said before the object is an instance of the class so now you can use this class at any time you want. so you just wanted to create a class instance and call the class members.

let’s take a look at how to create an instance of a class.

After creating an object you think that now how you can use the properties and methods of that class.  the following is the way where you can access all the public and protected members of the class.

Now the above example describes how you can access members of the specific class.  we just create an instance of the class in the $Test1 object variable.  and access all the members of the class using -> arrow. in the latest version of PHP help us to use Arrow to access all the members and functions in the class. now here you will see we access the methods and properties of the class using an arrow ( -> ).

You can also be store function value in the variable. Now I think you are able to use object-oriented programming with PHP.

I hope you understand the object-oriented programming with PHP.  now in the subsequent article, we will dive into functions and classes briefly.  the more article you read, the more you learn.

That is all for this article we will take you to the next one.