PHP Object Oriented

By | July 12, 2020
PHP Object Oriented

PHP supports the same OOPS concept as any other language like Java, C++, etc which allows it to build complex and reusable web applications. Some of the below OOPS concepts are explained in PHP.

  • Class
  • Object
  • Inheritance
  • Polymorphism
  • Overloading
  • Data abstraction
  • constructor
  • Destructor

1. Defining classes in PHP

A class can be considered as the user-defined data type that includes the functions, methods, and code block. These classes can be inherited to other classes and help in maintaining the code easy to handle and debug. Large codes can be divided into many classes that allow developers to understand the code easily.

Vamware

The common syntax of defining a class within PHP is given below-

<?php
      class demo_class {
        var $var1;
        var $var2 = "Hello";
      function demo_func ($arg1, $arg2) {
      [..]
      }
      [..]
     }
?>

The class keyword is used following the class name which includes the defined variables and their value. These classes may or may not include the functions that have the scope limited to that class and can be accessed using the instance of the class.

Class Example:

We created a class of demo_books as below:

<?php
   class Demo_books{
      /* Member variables */
      var $price;
      var $name;      
      /* Member functions */
      function setPrice($par){
         $this->price = $par; //$this refers to itself
      }      
      function getPrice(){
         echo $this->price ."<br/>";
      }      
      function setName($par){
         $this->name = $par;
      }      
      function getName(){
         echo $this->name ." <br/>";
      }
   }
?>

2. Creating objects within PHP

Objects are used to create the instance of the defined classes. Once the object is created you can access the variables and the functions of the class. You can create the object using the new keyword. You can create as many objects for a particular class and these objects are independent of each other.

Now we will create the object of the above-defined class ‘Demo_books’.

Object Example:

$physics = new Demo_books;
$maths = new Demo_books;

Accessing member functions

Once you have created the object for the class now you can access the defined functions using the objects.

Now we are calling the member functions and passing arguments accordingly.

Calling function Example:

Passing arguments:

$physics->setName( "Basic Physics" );
$chemistry->setName( "Advanced Physical Chemistry" );

$physics->setPrice( 10 );
$chemistry->setPrice( 15 );

Getting values from the functions-

$physics->getName();
$chemistry->getName();

$physics->getPrice();
$chemistry->getPrice();

Output:

Basic Physics

Advanced Physical Chemistry

10
15

1. Private members

If a variable or the function is defined as private then you are limiting its access means you cannot access those variables and functions outside that class. The private variables and functions cannot be inherited and overloaded by the child class.

2. Public members

Unlike private members, these members can be accessed from outside the class where it is defined. Even if the variable or function is defined as public they can be inherited and can be used by the implementing class.

3. Inheritance using PHP

Inheritance is an OOPS concept that allows a child class to inherit the properties, functions, and methods of the parent class, where a child class can redefine it again with a different implementation.

Inheritance Example:

<?php
class A {
    public function printItem($string) {
        echo 'Hi : ' . $string . "\n;
    }
    public function printPHP() {
        echo 'I am from valuebound' . PHP_EOL;
    }
}
class B extends A {
    public function printItem($string) {
        echo 'Hi: ' . $string . PHP_EOL;
    }
 public function printPHP() {
        echo "I am from ABC";
    }
}
$a = new A();
$b = new B();
$a->printItem('Raju');
$a->printPHP();       
$b->printItem('savan');
$b->printPHP();       
?>

Output-

Hi : Pavan
I am from valuebound
Hi: savan
I am from ABC

4. Overriding using PHP

Child class overrides the function definition of its parent class with the same name.

Overriding Example:

In the below example, the functions are redefined where they are returning some value.

function getPrice() {
   echo $this->price . "<br/>";
   return $this->price;
}   
function getName(){
   echo $this->name . "<br/>";
   return $this->name;
}

5. Constructor using PHP

These functions are used to called automatically upon the abject creation. The constructor can be parameterized or non-parameterized.

You can use the below syntax for creating _construct function-

function __construct( $par1, $par2 ) {
   $this->title = $par1;
   $this->price = $par2;
}

Constructor Example:

$physics = new Books( "Physics", 10 );
$chemistry = new Books ( "Chemistry", 15 );


$physics->getName();
$chemistry->getName();


$physics->getPrice();
$chemistry->getPrice();

Output:

Physics
Chemistry
10
15

6. Destructor using PHP

This function will allow you to release the used resources once your program gets executed. This can be using _desctruct function.

7. Interfaces using PHP

Interfaces are the type of the class which has the function name but not their implementation. The class that implements these interfaces have to provide the implementation. Also, these interfaces can be implemented by more than one classes. All the variables and the function are need to de defined public so that they can be implemented.

Interface Example:

interface Student {
   public function sendMail();
}

Then, if another class implemented that interface, like this −

class Report implements Student{
   // sendMail() Definition of the function
}

8. Constants using PHP

Unlike variables, these constants hold a value that is not changeable. You can define a constant using the const keyword.

Constant Example:

class MyClass {
   const Price = 1.7;   
   function __construct($Val) {
      //code to be executed
   }
}

9. Abstract classes using PHP

You cannot create the instance of the abstract class but you can inherit it from the parent class. You can use an abstract keyword to define your class as abstract.

Abstract Example:

abstract class Abstract_Demo {
   abstract function func_Abstract() {
   }
}

10. Static keyword using PHP

Once you declare the class or function as static there is no need to create the instance of the class to access them.

Static keyword Example:

<?php
   class Demo {
      public static $my_static = 'demo';      
      public function staticVal() {
         return self::$my_static;
      }
   }
   print Demo::$my_static . "\n";
   $demo = new Demo();   
   print $demo->staticValue() . "\n";
?>

Leave a Reply

Your email address will not be published. Required fields are marked *