DEV Community

loading...
Cover image for Reference to OOP PHP

Reference to OOP PHP

Salma
Wish to make our life easier with programming
Updated on ・5 min read

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

Classess

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:

class Fruit {
  // code goes here...
}
?>
Enter fullscreen mode Exit fullscreen mode

we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting the $name property:

class Fruit {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
?>
Enter fullscreen mode Exit fullscreen mode

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, $apple and $banana are instances of the class Fruit:

class Fruit {
  // Properties
  public $name;
  public $color;
 // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');
echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?> 
Enter fullscreen mode Exit fullscreen mode

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.

Constructor and descructor

A constructor allows you to initialize an object's properties upon creation of the object.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Notice that the construct function starts with two underscores (__)!

We see in the example below, that using a constructor saves us from calling the set_name() method which reduces the amount of
code:

class Fruit {
  public $name;
  public $color;
  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  function get_name() {
    return $this->name;
  }
  function get_color() {
    return $this->color;
  }
}
$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>
Enter fullscreen mode Exit fullscreen mode

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:

class Fruit {
  public $name;
  public $color;

  function __construct($name) {
    $this->name = $name;
  }
  function __destruct() {
    echo "The fruit is {$this->name}.";
  }
}
Enter fullscreen mode Exit fullscreen mode

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

class ClassName {
  public static $staticProp = "W3Schools";
}
?>
Enter fullscreen mode Exit fullscreen mode

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

Discussion (2)

Collapse
ayman_elmalah profile image
Ayman Elmalah

Good job salma

Collapse
salmazz profile image
Salma Author

thank u