Introduction
OOP stands for Object-Oriented Programming and it is used by almost all of the major programming languages. For example, PHP, Phyton, Java, C#, and many more. If you don't know what OOP is and how it works, don't worry. We are going to show you and explain what OOP is used for.
As OOP is so widely used, you should get yourself familiar with OOP's concept and how to use it yourself in your day-to-day coding.
In this tutorial, we will do a quick introduction to PHP OOP!
What is OOP?
As we said above, OOP stands for Object-Oriented Programming. In OOP, we combine a group of related variables and functions into a class. It is considered that it is more organized to use OOP, and it keeps us from the need to repeat the same code over and over again.
In the example below, we are going to show you a class and will explain everything in it as we go along.
class Hero {
public $name;
public $superPower;
public function intro() {
return 'This is ' . $this->name . ' and his power is ' . $this->superPower;
}
}
Don't worry if you have no idea what the code above is. We are going to explain everything. And now that we have created ourselves a class let's start by explaining what exactly is a class
.
What is a class
and how to create one
A class is what we store the variables/properties and functions/methods of a certain topic. We could make as many classes as we would like. And use every class for whatever topic we want.
This is how you define a class:
class NewUser {
}
We use the class
keyword to declare a class. Then we write the name of the class with upper camel case. What this means is that our first word starts with a capital letter, and every next word we use starts with a capital letter as well(NewUser, AbbeyRoad, HappyMeal). After that, we open and close curly braces, and inside those curly braces, we write our code.
Now let's learn about properties!
What are properties and how to add them
Properties are the variables in a class. So the properties in our Hero
class are $name
and $power
.
class Hero {
public $name;
public $superPower;
If you're wondering why there is a public
in front of our property, don't worry. This keyword is an access modifier and it helps us hide information.
We can also give the class properties all kinds of values. For example, strings, integers, booleans, etc.
There are precisely three access modifiers we can use. The first being the public
keyword, which makes it that any class can refer to the field or call the method. The second one - the private
keyword makes it that only the current class will have access to the field or method. And the last one is the protected
keyword, in which only the current class will have access to the field or method.
If you want to make a property that contains multiple words, you have to use lower camel case. This means that you have to start your word with a lowercase letter, and then every next word has to begin with a capital letter(newUser, abbeyRoad, happyMeal).
What are methods and how to add them
Methods are what we call the functions in a class. They also require an access modifier. As you can see in the method of our Hero
class.
class Hero {
public $name;
public $superPower;
public function intro() {
return 'This is ' . $this->name . ' and his power is ' . $this->superPower;
}
}
If you don't know what the $this
keyword is, be sure to check this post out. It will help you understand how it works. Also, notice how we don't put the $
sign in front of the property's name in our method. You must remember that.
https://devdojo.com/bobbyiliev/what-is-the-this-keyword-and-how-to-use-it-in-php
Now that we have explained what a class is and what it stores inside, let's explain what objects are and how they work!
What are objects, how they work, and how to create them
Objects are what we create for the class, and we give them different properties. So to create an object for a certain class, we need to use the new
keyword. As you can see below, this is how an object is created.
$batman = new Hero();
So now that we have created an object, we need to set its properties. So to do that, we have to point our object to the property that we want to set. So for our $batman
object, we are going to set it's $name
and $power
property.
$batman->name = 'Batman';
$batman->superPower = 'being rich';
Just like the properties in our method, we don't need to put a $
sign in front of the property.
If you would like to get the name of our object, we can just echo it out.
echo $batman->name;
This should print out:
Batman
And this is how your code should look like so far:
// First we define our class name
class Hero {
// Then we set our properties
public $name;
public $superPower;
// Here we define our first method
public function intro() {
return 'This is ' . $this->name . ' and his power is ' . $this->superPower;
}
}
// Here we do our instansiation
$batman = new Hero();
// We set the name property to 'Batman'
$batman->name = 'Batman';
// We define the superPower proerty:
$batman->superPower = 'being rich';
// Finally, we echo out the name property of the object
echo $batman->name;
And now for us to call our method, we have to set the properties of our object, and then we can call the method.
We call the method the same way we call our property:
echo $batman->intro();
And this should print out:
This is Batman and his power is being rich
Conclusion
OOP is definitely going to be a bit tough in the beginning, but in the end, when you start understanding it and start using it in your code.
I hope that this post helped you better understand the basics of PHP OOP!
Comments (0)