- Joined
- 11 yrs. 6 mth. 27 days
- Messages
- 5,381
- Reaction score
- 18,380
- Age
- 45
- Wallet
- 11,590$
- [email protected]
So, have you ever heard of the term OOP or Object Oriented Programming? Ever found a highlighted key-word "Class" in one of the open source projects on websites like github?
Well, fear not, I will explain briefly and with demonstrations as to what OOP is in relation to PHP.
What is OOP?
A simple Metaphor
There isn't really a solid or hard written definition of what OOP is, so at the end of the tutorial feel free to share your own definition.
Object Oriented programming simulates the blueprint to factory concept. You map out a blue print of how a product is created and you simply put it into production within the factory. Maybe more rather as you'd create a robot, you map out a blue print of the different parts - the microprocessor, the type of metal needed for the body and the number of wires- The previous is known as Properties; Properties in short are the raw materials the blue print will require to come true e.g. metal plate.
Now, the robot is finished and we're inputting instructions into the microprocessor to begin working like any other decent robot, you input the instruction walk, stop, alarm, shutdown. These are known as methods, or functions performed by the robot.
The real thing
Object Oriented programming is a concept that treats functions and data as objects. But, before going all out on me, carry on reading, as I mentioned earlier, OOP has no solid definition henceforth, you are free to adopt or create a definition that best suits you and keeps you in the perfect mental state.
OOP is rather a concept of programming and not a rule. OOP gives you a god-like feeling when dealing with web applications(since we're talking about PHP). You map the blueprint and create an object with all the flexibility you wish for!
If you're interested, carry on, otherwise, carry on as well, you might learn a thing or two!
I don't believe in robots, what's your point?
Well that's rather disappointing! Well, how is this related to php you ask? When PHP was first introduced and up till version 4 it had few support to OOP and it wasn't encouraged to work with until the glorious PHP 5 was released.
Now, let's get to real programming!
What's an object? An object is a programming entity that holds all properties and methods described in a class. A normal human being is an object. A common blueprint of a human being has properties such as eye color or hair color and methods such as walk or talk or run.
An object in PHP isn't so different, it is the same, infact, you can create a human in PHP. This looks like:
If you execute that, you would get the following message: "Human has walked!".
This indicates that $zentrixPlus has successfully taken a walk...
Is it any different than setting the eye color? Not a bit, the difference that PHP takes for granted is the parentheses following the walk method (if you leave them out PHP thinks you're referring to a variable/property and throws an unexpected T_VARIABLE error or an error along the lines.). You write it after the object operator just as you write out a function ( speak() ).
Summary
That is really about it for the basics of OOP, to wrap this up, these are the main points:OOP is a concept that revolves around creating objects using blueprints aka Classes in programming.
The keyword Class is used to start a class, it formats as: Class myClass{ //code here }
A property is a vaiable that is specified/related to the object it describes.
A method is a function that tells the object what to do.
To create a new object, specify a vaiable and equate it to new myClass i.e. $object = new myClass;
"->" is an object operator that is used to refer to a property or a method inside the object.
Please leave a feedback on what you thought about the tutorial. Did you like it? Did you despise it? Or somewhere in the middle? Let me know if you're interested in more OOP.
NOTE: Variables are accessed without the $ sign, e.g. $object->var -NOT- $object->$var.
NOTE: the keyword var that is used in the class example will be explained in the next tutorial. Not using that keyword will throw an error.
Well, fear not, I will explain briefly and with demonstrations as to what OOP is in relation to PHP.
What is OOP?
A simple Metaphor
There isn't really a solid or hard written definition of what OOP is, so at the end of the tutorial feel free to share your own definition.
Object Oriented programming simulates the blueprint to factory concept. You map out a blue print of how a product is created and you simply put it into production within the factory. Maybe more rather as you'd create a robot, you map out a blue print of the different parts - the microprocessor, the type of metal needed for the body and the number of wires- The previous is known as Properties; Properties in short are the raw materials the blue print will require to come true e.g. metal plate.
Now, the robot is finished and we're inputting instructions into the microprocessor to begin working like any other decent robot, you input the instruction walk, stop, alarm, shutdown. These are known as methods, or functions performed by the robot.
The real thing
Object Oriented programming is a concept that treats functions and data as objects. But, before going all out on me, carry on reading, as I mentioned earlier, OOP has no solid definition henceforth, you are free to adopt or create a definition that best suits you and keeps you in the perfect mental state.
OOP is rather a concept of programming and not a rule. OOP gives you a god-like feeling when dealing with web applications(since we're talking about PHP). You map the blueprint and create an object with all the flexibility you wish for!
If you're interested, carry on, otherwise, carry on as well, you might learn a thing or two!
I don't believe in robots, what's your point?
Well that's rather disappointing! Well, how is this related to php you ask? When PHP was first introduced and up till version 4 it had few support to OOP and it wasn't encouraged to work with until the glorious PHP 5 was released.
Now, let's get to real programming!
What's an object? An object is a programming entity that holds all properties and methods described in a class. A normal human being is an object. A common blueprint of a human being has properties such as eye color or hair color and methods such as walk or talk or run.
An object in PHP isn't so different, it is the same, infact, you can create a human in PHP. This looks like:
If you execute that, you would get the following message: "Human has walked!".
This indicates that $zentrixPlus has successfully taken a walk...
Is it any different than setting the eye color? Not a bit, the difference that PHP takes for granted is the parentheses following the walk method (if you leave them out PHP thinks you're referring to a variable/property and throws an unexpected T_VARIABLE error or an error along the lines.). You write it after the object operator just as you write out a function ( speak() ).
Summary
That is really about it for the basics of OOP, to wrap this up, these are the main points:OOP is a concept that revolves around creating objects using blueprints aka Classes in programming.
The keyword Class is used to start a class, it formats as: Class myClass{ //code here }
A property is a vaiable that is specified/related to the object it describes.
A method is a function that tells the object what to do.
To create a new object, specify a vaiable and equate it to new myClass i.e. $object = new myClass;
"->" is an object operator that is used to refer to a property or a method inside the object.
Please leave a feedback on what you thought about the tutorial. Did you like it? Did you despise it? Or somewhere in the middle? Let me know if you're interested in more OOP.
NOTE: Variables are accessed without the $ sign, e.g. $object->var -NOT- $object->$var.
NOTE: the keyword var that is used in the class example will be explained in the next tutorial. Not using that keyword will throw an error.