Object Oriented programming - Kussin | Development Unit Macedonia

Object Oriented programming


Object Oriented programming is a programming technique or paradigm that makes use of classes and objects to write application programs.

06.01.2023 | Ilir Raufi | Blog, Development

OOP is a programming technique or paradigm that makes use of classes and objects to write application programs. In an object-oriented program, everything is viewed as a real world object with attributes and behaviours . OOP is a technique that is extensively used in most modern programming languages, it opens the door to cleaner code designs, easier maintenance, more code reuse, and has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP 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

Object Oriented Concepts

Lets define important terms related to Object Oriented Programming.

Class : 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.

The general form for defining a new class in PHP is as follows.

<?php
class phpClass {
var $var1;
var $var2 = “constant string”;

function myfunc ($arg1, $arg2) {

}
}
?>

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 are created using the new keyword.

In the example below, $apple and $banana are instances of the class Fruit:

The $this keyword refers to the current object, and is only available inside methods.

Properties are special variables that are defined in a class.

A property, also known as a member variable, holds data that can vary from object to object. A property in a class looks similar to a standard PHP variable except that, in declaring a property, you must precede the property variable with a visibility keyword. This can be public, protected, or private, and it determines the scope from which the property can be accessed.

Methods are special functions declared within a class. Just as properties allow your objects to store data, methods allow your objects to perform tasks or actions. A method declaration resembles a function declaration. The function keyword precedes a method name, followed by an optional list of argument variables in parentheses.

Constructor Functions

Constructor Functions are special type of functions which are called automatically whenever an object is created.

PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.

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

The destruct Function

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.

OOP – Inheritance

Inheritance in OOP = When a class derives from another class.

PHP class definitions can optionally inherit from a parent class definition by using the extends clause.

An inherited class is defined by using the extends keyword.

The syntax is as follows:

class Child extends Parent {
// code goes here…
}

The effect of inheritance is that the child class has the following characteristics :

  • Automatically has all the member variable declarations of the parent class.
  • Automatically has all the same member functions as the parent, which (by default) will work the same way as those functions do in the parent.

Static methods

Static methods are declared with the static keyword, and can be called directly without creating an instance of the class first.

To access a static method use the class name, double colon (::), and the method name : ClassName::staticMethod(); 

Error: Contact form not found.