
Written By
Object Oriented Programming
Object Oriented Programming
“Using PHP”
Object-Oriented Programming, also known as OOP is a special way of programming. It is considered to be more powerful and fast for certain tasks than the normal way of programming in PHP. OOP helps you to create and manage tasks easily.
Some advantages of OOP:
-
Easy to manage
-
Easy to use
-
Prevents repetition
-
Fast and efficient
OOP is harder to understand compared to other programming techniques. But, if you understand the following 4 terms you are almost done!
Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.
What Is the MVC Model | MVC Model Explained?
Model: Structures your data in a reliable form and prepares it based on controller’s instructions
View: Displays data to user in easy-to-understand format, based on the user’s actions
Controller: Takes in user commands, sends commands to the model for data updates, sends instructions to view to update interface.
Here is how the same process plays out in a modern web app:
The user makes a request along a route, let’s say /home.
The controller receives this request and gives a specific set of orders that are related to that route. These instructions could either be for the view to update or serve a certain page, or for the model to perform specific logic. Let’s assume this request has some logic associated with it.
The model carries out the logic, pulls from a database and sends back a consistent response based on the controller’s instructions.
The controller then passes this data to the view to update the user interface.
Classes
A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces:
Example:
We declare a class named Car consisting of two properties ($model
And $color) and two methods set_model() and get_model() for setting and getting the $model property:
Object
We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.
Objects of a class is created using the new keyword.
In the example below, $bmw and $audi are instances of the class Car:
OOP SCOPES
we have three keys write before properties and methods in the oop
the three keys is define what is the scope of property and method , if i can use it in another class or in the same class ,
it just like if some take key to open the room but not take the key for open another room.
There are three access modifiers:
Public -scope to make that property/method available from anywhere, other classes and instances of the object.
Protected - scope when you want to make your property/method visible in all classes that extend current class including the parent class.
Private - scope when you want your property/method to be visible in its own class only.
A destructor is called when the object is destructed or the script is stopped or exited.
If you create a __destruct() function, PHP will automatically call this function at the end of the script.
Notice that the destruct function starts with two underscores (__)!
The example below has a __construct() function that is automatically called when you create an object from a class, and a __destruct() function that is automatically called at the end of the script:
STATIC METHODS AND PROPERTY
A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both. The variables and methods are accessed without the creation of an object, using the scope resolution operator(::). But here is a catch, that the static method cannot access the non-static variables because that will require the creation of the object first. So, to access variables of a static class we must declare them as static using keyword static.
When to use what?
Consider using Static class if any of these statements apply to situation:
Functionality of the methods and variables of a class are general (global).
When you want a respective field or variable to have same value cross-instance or when you want single instance of that class.
When creating a singleton (particular kind of class that can be instantiated only once.) or a utility (helper) class.
When each object is having same data and require to create a class that just works only on this data, then static class can be used.
PHP - Static Properties
Static properties can be called directly - without creating an instance of a class.
Static properties are declared with the static keyword:
Public static variable acts like global variable.
Example:
Here, we declare a static property: $value. Then, we echo the value of the static property by using the class name, double colon (::), and the property name (without creating a class first).
SELF AND THIS
Use $this to refer to the current object. Use self to refer to the current class. In other words, use $this->member for non-static members, use self::$member for static members.
Waiting us in the next parts “have a nice code”.