PHP

Getting Started with Objected Oriented Programming in PHP 7

PHP, an open source language, allow programmers to quickly develop error-free programs using procedural and objected-oriented programming techniques.

Procedural programming

A procedural programming includes functions (methods) that can be called from the main flow of the program. The flow of the program jumps to the function (method), executes the code within the module, and then returns to the next statement in the main flow of the program.

Object-oriented programming (OOP)

OOP Class Image

OOP is a design philosophy that uses objects and methods rather than linear concepts of procedures and tasks (procedural programming) to accomplish programmatic goals. An object is a self-sustainable construct that enables re-usability of code. A method specifies one operation without providing any details to describe how the operation should be carried out.

Advantages of OOP

Object-oriented programming (OOP) has some advantages over procedural programming: modularity, code re-usability, information hiding, and debugging ease.

Modularity

OOP provides a clear modular structure for programs. Modularity refers to the concept of making multiple modules first and then linking and combining them to form a complete system. Modularity enables re-usability and minimizes duplication.

Reusability

Code can be re-use, without modification, to perform a specific service regardless of what application uses the code.

Information-hiding

The detail of internal implementation of a module (class) remain hidden from the outside world.

Debugging

Easier to fix problems because the module (class) is independent from other pieces of code. Modifying one piece of code doesn’t impact other pieces of code in the application.

OOP fundamentals

Class

A class is a template for creating objects.

In PHP a class is a collection of variables and functions working with these variables.

To define a class, the class keyword is used, followed by a name and a code block delimited by curly braces.

class User {

}

Properties

Class member variables are called properties

In PHP, class member variables are called “properties” (also “attributes” or “fields”). They are defined by using one of the keywords public, protected or private, followed by a normal variable declaration.

class User{
 public $name;
}

Method

A method is a procedure associated with an object (similar to PHP functions)

A function (method) is a piece of code which may take some input in the form of parameter and does some processing and returns a value.

In OOP, a method is a part of a class and included in any object of that class. You can define method by using one of the keywords public, protected or private, followed by a normal name declaration.

class User{
 public $name;

 public function setName(){

 }

}

Properties and methods visibility

The property defined in above class were tagged as public. That means that it is accessible to anyone, or more precisely, from anywhere. This is called the visibility of the property or method.

private visibility

This type allows access only to members of the same class. Members declared as private may only be accessed by the class that defines the member.

Use this type if you want variable/function (method) to be visible in its own class only.

class User{
 //can be accessible within the same class
 private $name;
}
protected visibility

This type allows access to members of the same class and instances from classes that inherit from that one only.

Use this type if you want to make variable/function (method) to be visible in all classes that extend current class including the parent class.

class User {
 //accessible for same class and its child classes
 protected $name;
}
public visibility

This type refers to a property or method that is accessible from anywhere. Any classes or code in general from outside the class can access it.

Use this type if you want to make variable/function available from anywhere, other classes and instances of the object.

class User{
 //accessible from anywhere
 public $name;
}

Object

An instance of a class is called object.

Object refers to a particular instance of a class where the object can be a combination of variables, functions (methods), and data structures.


// Instance (or object) of User class
$user = new User();

Creating a PHP class : example

<?php

class User {

}

Set private properties

Private visibility: Fatal error if try to access from outside the class

<?php

class User {
 private $name;
 private $gender;
}

Set public properties

Public visibility: can be access and modify from anywhere

<?php

class User {
 private $name;
 private $gender;
 public $age;
}

Set initialized properties (or properties with default value)

<?php

class User {
 private $name;
 private $gender;
 public $age;
 private $department = 'Mechanical';
}

Set class methods

<?php

class User {
 private $name;
 private $gender;
 public $age;
 private $department = 'Mechanical';
 
 /* set value to 'private $name' property */
 public function setName($name){
  $this->name = $name;
 }
 
 /* get value from 'private $name' property */
 public function getName(){
  return $this->name;
 }
}

The complete class

<?php

class User {
 private $name;
 private $gender;
 public $age;
 private $department = 'Mechanical';
 
 public function setName($name){
  $this->name = $name;
 }
 public function getName(){
  return $this->name;
 }

 public function setGender($gender){
  $this->gender = $gender;
 } 
 public function getGender(){
  return $this->gender;
 }

 public function setDepartment($dep){
  $this->department = $dep;
 }
 public function getDepartment(){
  return $this->department;
 }
}

Making instances of the class

$user = new User();

Assign value to instance variables

$user->setName('Sheri');
$user->setGender('F');

//public property : no error, accessible from anywhere
$user->age = 30; 

//Fatal error: Cannot access private property
//Private and protected methods can not be access directly
$user->department = 'Computer';

//The right way to assign value for protected and private methods
$user->setDepartment('Computer');

Fetch and print instance variables value

echo $user->getName();
echo $user->getDepartment();
echo $user->age;

//Fatal error: Cannot access private property User::$department
echo $user->department;

/* You can make as many instance as you want */
$user2 = new User();

//prints Mechanical (default value for all instances)
echo $user2->getDepartment();

//changed the default department
$user2->setDepartment('Computer');
by BrainBellupdated
Advertisement: