Object-Oriented Software Engineering in PHP
If You've decided to learn PHP OOP, it’s a good
choice!
Here is a brief overview of this unique way of
thinking before you dive into its large area:
1) What's OOP?
2) What it covers?
3) What makes it unique from the methods you use to create your website?
4) How is my code represented?
Let's start by asking
ourselves those questions above.
The solution is original because you employed
"procedural representation," which involves isolating the data's processing
from the data itself. For instance, your website has a news section.
The data (such as the news, a list of mistakes, a link
to the BDD, etc.) is on one side, and the instructions that change the data are
on the other.
If I'm not mistaken, they are code in this manner.
You may feel that this method of presenting
application is the finest because it is the only one you are familiar with.
Besides, you don't really see how your code could be represented
in a different way.
Well that time of ignorance is over here comes object-oriented
programming!
So, what is this way of representing its code? making your website a collection of
interconnected objects is all that OOP entails.
In other words: everything is an object. I'm sure you
already know what it is.
In addition, you have a lot of them nearby, including perhaps a computer, a lamp, a chair, a desk, and other things. All of them are object.
Objects and code are essentially the same thing.
The most relevant example when doing a course on OOP
is to use the example of the character in a fighting game.
Thus, imagine we have a Character object in our
application. A character has characteristics:
· strength;
· 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, We will onto
them later.
Aside from these characteristics, a character also has
abilities. He can:
· hit another character;
· gain experience;
· move.
These capacities correspond to functions. As for the
variables, these are somewhat special functions.
You now know that you can have objects in an application.
*But 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, these information is contained in what are
called classes.
What
so called Class?
A class is an entity grouping variables and functions.
Each of these functions will have access to the variables of this entity.
What's
an instance?
An instance is simply the result of an instantiation.
An instantiation is an act of instantiating a class.
To instantiate a class is to use a class so that it
creates an object for us. Basically, an instance is an object.
Basic
Syntax of creating Class in PHP
The declaration of attributes in a class is done by
writing the name of the attribute to be created, preceded by its visibility.
Visibility
of an attribute or method
The visibility of an attribute or method indicates
where it can be accessed from. We will see here two types of
· visibility:
· public and private.
The first, public, is the simplest. If an attribute or
method is public, then it can be accessed from
anywhere, from inside the object (in the methods we
created), as well as from the outside.
The second, private, imposes some restrictions.
We will only have access to attributes and methods
from inside the
class, i.e. only code wanting to access a private attribute
or private method written inside the class
will work.
Otherwise, a pretty fatal…
You can see that each attribute is preceded by an
underscore ("_").
This is a notation that is better to respect (this is
PEAR notation) which says that each private element name (here these are
attributes, but we'll see more later that it can also be methods) must be
preceded by an underscore.
Thanks for reading..................
Thanks
ReplyDelete