PHP and OOP
- Internal Links:
- External Links:
1 - PHP - What is OOP?
- From PHP5, you can also write PHP code in an object-oriented style.
- Object-Oriented programming is faster and easier to execute.
PHP - What is OOP?
- Go to Top
- OOP stands for Object-Oriented Programming.
- Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.
- Object-oriented programming has several advantages over procedural programming:
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP helps to keep the PHP code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time
- 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.
PHP - What are Classes and Objects?
- Classes and objects are the two main aspects of object-oriented programming.
- Look at the following illustration to see the difference between class and objects:
class
Fruit |
objects
Apple Banana Mango |
class
Car |
objects
Volvo Mazda Toyota |
- Go to Top
- So, a class is a template for objects, and an object is an instance of a class.
- When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
- Look at the next chapters to learn more about OOP.
2 - PHP OOP - Classes and Objects
- A class is a template for objects, and an object is an instance of class.
OOP Case
- Go to Top
- Let's assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.
- When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
Define a Case
- Go to Top
- 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:
- Below 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:
- Note: In a class, variables are called properties and functions are called methods!
Define Objects
- Go to Top
- Classes are nothing without objects! 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:
- Go to Top
- In the example below, we add two more methods to class Fruit, for setting and getting the $color property:
PHP - The $this Keyword
- The $this keyword refers to the current object, and is only available inside methods.
- Look at the following example:
- So, where can we change the value of the $name property? There are two ways:
- 1. Inside the class (by adding a set_name() method and use $this):
- 2. Outside the class (by directly changing the property value):
PHP - instanceof
- Go to Top
- You can use the instanceof keyword to check if an object belongs to a specific class:
3 - PHP OOP - Constructor
PHP - The __construct Function
- Go to Top
- 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:
4 - PHP OOP - Destructor
PHP - The __destruct Function
- Go to Top
- 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:
- Tip: As constructors and destructors helps reducing the amount of code, they are very useful!
5 - PHP OOP - Access Modifiers
PHP - Access Modifiers
- Go to Top
- Properties and methods can have access modifiers which control where they can be accessed.
- There are three access modifiers:
- public - the property or method can be accessed from everywhere. This is default
- protected - the property or method can be accessed within the class and by classes derived from that class
- private - the property or method can ONLY be accessed within the class
- In the following example we have added three different access modifiers to three properties (name, color, and weight).
- Here, if you try to set the name property it will work fine (because the name property is public, and can be accessed from everywhere).
- However, if you try to set the color or weight property it will result in a fatal error (because the color and weight property are protected and private):
- In the next example we have added access modifiers to two functions.
- Here, if you try to call the set_color() or the set_weight() function it will result in a fatal error (because the two functions are considered protected and private), even if all the properties are public:
6 - PHP OOP - Inheritance
PHP - What is Inheritance?
- Go to Top | w3schools
- Inheritance in OOP = When a class derives from another class.
- The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods.
- An inherited class is defined by using the extends keyword.
- Let's look at an example:
7 - PHP OOP - Constants
PHP - Class Constants
- Go to Top | w3schools
- Constants cannot be changed once it is declared.
- Class constants can be useful if you need to define some constant data within a class.
- A class constant is declared inside a class with the const keyword.
- Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.
- We can access a constant from outside the class by using the class name followed by the scope resolution operator (::) followed by the constant name, like here:
8 - PHP OOP - Abstract Classes
PHP - What are Abstract Classes and Methods?
- Go to Top | w3schools
- Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks.
- An abstract class is a class that contains at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.
- An abstract class or method is defined with the abstract keyword:
9 - PHP OOP - Interfaces
PHP - What are Interfaces?
- Go to Top | w3schools
- Interfaces allow you to specify what methods a class should implement.
- Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism".
- Interfaces are declared with the interface keyword:
10 - PHP OOP - Traits
PHP - What are Traits?
- Go to Top | w3schools
- PHP only supports single inheritance: a child class can inherit only from one single parent.
- So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.
- Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
- Traits are declared with the trait keyword:
11 - PHP OOP - Static Methods
PHP - What are Static Methods?
- Go to Top | w3schools
- Static methods can be called directly - without creating an instance of the class first.
- Static methods are declared with the static keyword:
12 - PHP OOP - Static Properties
PHP - What are Static Properties?
- Go to Top | w3schools
- Static properties can be called directly - without creating an instance of a class.
- Static properties are declared with the static keyword:
13 - PHP OOP - Namespaces
PHP - What is Namespaces?
- Go to Top | w3schools
- Namespaces are qualifiers that solve two different problems:
- They allow for better organization by grouping classes that work together to perform a task
- They allow the same name to be used for more than one class
- For example, you may have a set of classes which describe an HTML table, such as Table, Row and Cell while also having another set of classes to describe furniture, such as Table, Chair and Bed. Namespaces can be used to organize the classes into two different groups while also preventing the two classes Table and Table from being mixed up.
14 - PHP OOP - Iterables
PHP - What is an Iterable?
- Go to Top | w3schools
- An iterable is any value which can be looped through with a foreach() loop.
- The iterable pseudo-type was introduced in PHP 7.1, and it can be used as a data type for function arguments and function return values.
Reserve