Object Oriented Programming in PHP : All OOP Concepts

Object

Objects is collection of member variables(member data and member function)

Class

Class is group of objects with same attributes and common behaviors

Inheritance

Inheritance is the process of driving a new class from existing class,inherit the all features(Methods and Properties) of base class(Parent Class) in child class with extra features

Scope resolution operator

Scope resolution operator in oop with php : by Scope resolution operator(::) access to static, constant, and overridden properties or methods of a class from outside the class by class name.

Final keyword in php

  • Prevents child classes from overriding a method if method is declared as final.
  • If the class defined as final then it cannot be extended.
  • A variable(Properties) cannot be declare as final

Final methods example

<?php
//Base class
class PHP {
   final public function finalTest() {
       echo "Base Class is PHP";
   }
}

//Child class
class MYSQL extends PHP {
   public function finalTest() {
       echo "Child Class is MYSQL";
   }
}
?>
//Output
Fatal error: Cannot override final method PHP::finalTest() 

Final class example

<?php
//Base class
final class PHP {
   final public function finalTest() {
       echo "Final Base Class is PHP";
   }
}

//Child class
class MYSQL extends PHP {
   public function finalTest() {
       echo "Child Class is MYSQL";
   }
}
?>
//Output
Fatal error: Class MYSQL may not inherit from final class (PHP)