Introduction to OOP
Sign in or register for free to take advantage of all the features of this course!
So that's it, you decided to get started in the OOP in PHP? Wise decision ! We will dive into this vast area with an introduction to this new way of thinking: what is OOP? What is it about ? How is this different from the method you use to develop your website? So many questions that I will answer.
However, since I know you can not wait to get started, we are going to start things seriously by creating our first class at the end of this chapter. You will begin your first steps in the OOP in PHP!
What is OOP?
Let's start this course by asking you a question: how is your code represented? The answer is unique: you have used the "procedural representation" of separating data processing from data itself. For example, you have a news system on your site. On the one hand, you have the data (the news, a list of errors, a connection to the database, etc.) and on the other side you have a series of instructions that modify these data. If I'm not mistaken, that's the way you code.
This way of representing your application seems probably the best since it is the only one you know. Besides, you do not really see how your code could be represented in a different way. Well, this era of ignorance is over: here is object-oriented programming!
Then came object-oriented programming
So what is this way of representing his code? OOP is simply making your site a set of objects that interact with each other. In other words: everything is object.
Definition of an object
I'm sure you know what it is. Besides, you have quite a lot beside you: I'm sure you have a computer, a lamp, a chair, an office, or whatever. These are all objects. In programming, objects are essentially the same thing.
The most relevant example when doing a course on OOP is to use the character's example in a fighting game. So, let's imagine that we have a PersonObject in our application. A character has characteristics:
- a force ;
- a location;
- some experience;
- and finally damage.
All its characteristics correspond to values. As you probably know, values are stored in variables. This is always the case in OOP. These are somewhat special variables, but we will come back to this later.
Aside from these characteristics, a character also has abilities. He can :
- to strike another character;
- gain experience ;
- move.
These abilities correspond to functions. As with variables, these are special features and we will come back to them in due course. In any case, the principle is there.
You now know that you can have objects in an application. Where do they come from? In real life, an object does not come out of nowhere. Indeed, each object is defined according to characteristics and a very precise plan. In OOP, this information is contained in so-called classes.
Definition of a class
As I just said, classes contain the definition of objects that we will create later. Take the simplest example in the world: cakes and their mold. The mold is unique. It can produce an infinite amount of cakes. In this case, the cakes are the objects and the mold is the class: the mold will define the shape of the cake. The class thus contains the plan of manufacture of an object and one can use it as much as one wants in order to obtain an infinity of objects.
Concretely, a class, what is it?
A class is an entity that groups variables and functions. Each of these functions will have access to the variables of this entity. In the case of the character, we will have a functionfrapper (). This function will simply have to modify the character's $ damage variable according to the $ force variable. A class is therefore a logical grouping of variables and functions that any object resulting from this class will have.
Definition of an instance
An instance is simply the result of an instantiation. An instantiation is instancing a class. Instantiating a class is using a class to create an object. Basically, an instance is an object.
Submitted On : 2015-12-09
Taille : 168.19 Kb
Downloads : 679
Submitted On : 2015-12-09
Taille : 487.83 Kb
Downloads : 1220
Submitted On : 2019-05-02
Taille : 0.87MB
Downloads : 261
Submitted On : 2022-02-02
Taille : 6.0 MB
Downloads : 48
Submitted On : 2022-02-03
Taille : 6.7 MB
Downloads : 113
Submitted On : 2022-06-30
Taille : 0.13 MB
Downloads : 99