Categories
PHP

Object Oriented Programming

Object-oriented programming assumes everything as an object. This tutorial covers how to declare, implement, and use classes.

PHP allows programmers to quickly develop error-free programs using procedural and objected-oriented programming techniques:

  • Procedural programming includes functions (or methods) that can be called from the main flow of the program. The flow of the program jumps to the function (or method), executes the code within the module, and then returns to the next statement in the main flow of the program, as we see in previous tutorials.
  • Object-oriented programming (OOP) assumes everything as an object. One of the key features of OOP is the ability to create new data types in which the data and the implementation of operations are bound together. You create classes that contain the data representing the properties of a particular item, and the set of functions that can be performed on it. You can also extend object to create new variations on the type and shares code within them.

Fundamentals

In object-oriented programming, you write code to define new data types called classes. These classes contain properties that represent the fundamental information needed to describe an item. Then you associate operations, called methods or member functions, which you would normally want to perform on those items. When you create an instance of a class, you have created an object.

Class

A class is a template for creating objects. To define a class, the class keyword is used, followed by a name and a code block delimited by curly braces:

<?php
 class User {
  // contents, including properties and methods, go here
 }

Properties

Class member variables are called properties (also “attributes” or “fields”). Properties are defined by using one of the visibility modifiers keywords public, protected, or private, followed by a normal variable declaration:

<?php
 class User {
  public $username;
 }

You can also create properties with default values:

<?php
 class User {
  public $username = 'Guest';
 }

Brief description of visibility modifiers:

  1. public: anyone can access it.
  2. private: only members of the same class can access it.
  3. protected: members of the same class and child class can access it.

For detail visit Visibility/Access Modifiers in PHP tutorial.

Method

A method is a function associated with an object. It is a piece of code that may take some input in the form of parameters and does some processing and returns a value. You can define methods by using one of the keywords public, protected, or private, followed by a normal function name declaration:

<?php
 class User {
  public $username = 'Guest';
  public function setUsername($u) {
   return $u;
  }
 }

$this

To access a member variable from within a member function, you must use the special variable (also called a pseudo-variable) $this. It refers to the current instance of the object itself. You combine it with the -> operator to access the member variables:

<?php
 class User {
  public $username = 'Guest';
  public function setUsername($u) {
   return $this->username = $u;
  }
 }

Object

To use the data structures and functions defined in a class, an instance of the class, an object, needs to be created. Like other data types: integers, strings, arrays, and so on, objects are held by variables. However, unlike other types, objects are created using the new operator. An object of class User can be created and assigned to a variable as follows:

<?php
 $user = new User();

Once the variable $user is created, the member variables and methods (functions) of the new object can be used. Members of the object, both variables, and functions, are accessed using the -> operator. Consider the following example:

<?php
 class User {
  public $username = 'Guest';
  public function setUsername($u) {
   $this->username = $u;
  }
 }
 //Creating object
 $user = new User();
 
 //Accessing method
 $user->setUsername('Admin');

 //Accessing property
 echo $user->username; 

Many objects of the same class can be created. For example, you can use the following fragment to create three objects and assign them to three variables:

<?php
 /*Class code 
   ....       */
 $user1 = new User();
 $user2 = new User();
 $user3 = new User();

 $user1->setUsername('Admin');
 $user2->setUsername('brainbell');
 $user3->setUsername('user3');

The variables $user1, $user2, and $user3 are different. Each variable is of type object and references an object of the class User, but the objects themselves are independent.

Type Declaration

PHP takes a dynamic approach to typing and doesn’t require us to declare types when you want to use variables or add parameters to functions. However, since version 7, PHP offers the possibility of strict type function arguments, return values, and class properties (PHP 7.4), known as “Type declarations”. Visit the following links for detail:

Type Declaration Example:

<?php
 class User {
  public $username;
  
  # parameter TYPE declaration - String
  public function setUsername(String $u) {
   $this->username = $u;
  }
  
  # return TYPE declaration - String
  public function getUsername() : String {
   return $this->username;	  
  }
 }
 $user = new User();
 $user->setUsername('Admin');
 echo $user->getUsername(); 

PHP OOP Tutorials: