I want to create my new project by using Object Orientated Programing, so I have done some research into using OOP in PHP. So, from the articles I have read and the videos I have watched, this is my explanation of OOP.
Object Orientated Programing (OOP) is a way to group code together by a common theme for reusablity. You create objects that have attributes and actions similar to a physical object. Creating code in this way makes it easier to read the code and it also makes the code more modular.
For example a chair has legs, a seat, a back, and a color and it can hold a person. The chair is the object, its characteristics are the attributes, and it’s actions are methods.
class Chair {
var $legs;
var $back;
var $color;
function SitOn( ) {
echo "A person is sitting on this chair.";
}
function GetUp( ) {
echo "A person got out of the chair";
}
}
Creating a class does not add a chair to the project. It is like a blueprint for a class. The class shows what a chair would look like if there were a chair. To have a chair we need to create a chair from the blueprint. This is called Instantiation or creating an instance.
$chair = new Chair;
After the chair is created we can set the attributes and call the methods.
$Chair->color = "blue";
$Chair->legs = "4";
$Chair->SitOn();
These properties can also be set by default within the class. Then each new chair will automatically have these properties.
class Chair {
var $legs = "4";
var $back;
var $color = "4";
function SitOn( ) {
echo "A person is sitting on this chair.";
}
function GetUp( ) {
echo "A person got out of the chair";
}
}
Each new chair will have four legs and will be blue.
The variables and methods of a class can be limited with access modifiers.
class Chair {
Public $legs = "4";
Private $back;
Public $color = "4";
Public function SitOn( ) {
echo "A person is sitting on this chair.";
}
Protected function GetUp( ) {
echo "A person got out of the chair";
}
}
There is one more access modifier called static. The static modifiers belong to the class and not to the instance. For example you may want to have a variable that holds the current number of chairs.
class Chair {
Public $legs = "4";
Private $back;
Public $color = "4";
Static $totalChairs = "7"
Public function SitOn( ) {
echo "A person is sitting on this chair.";
}
Protected function GetUp( ) {
echo "A person got out of the chair";
}
}
These are accessed within your code by using the class name a double colon.
echo Chair::$totalChairs;
We can also set up methods that will run instantly on Instantiation or on the destruction of the instance. These are called constructors and destructors.
class Chair {
Public $legs = "4";
Private $back;
Public $color = "4";
Static $totalChairs = "7"
function __construct() {
echo "Your have created a new chair";
self::$counter++;
}
Public function SitOn( ) {
echo "A person is sitting on this chair.";
}
Protected function GetUp( ) {
echo "A person got out of the chair";
}
function __destruct() {
echo "Your have destroyed the chair";
self::$counter--;
}
}
These are some of the basic principles of OOP in PHP. There is more to it and I hope to learn more as I start to implement it in my project.