OOPs Concepts – PHP

OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually a very straightforward and ultimately simpler approach to programming.

Before we go in details, let’s define important terms related to Object Oriented Programming.

  • Class− Simply Class is a collection of member variables and its functions.
  • Object−You define a class once and then make many objects that belong to it. Objects have state behavior and identity.
  • Member functions and Variables− these are the functions and variables defined inside a class and are used to access object data.
  • Inheritance– Simply creating a new class from the existing class. When a class is defined by inheriting existing function of a parent class then it is called inheritance.
  • Polymorphism– Using this you can create multiple functions with their same name.  Or simply saying function name will remain same but it makes take different number of arguments and can do different task.
  • Overloading− a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.
  • Data Abstraction− Any representation of data in which the implementation details are hidden (abstracted).
  • Encapsulation− refers to a concept where we encapsulate all the data and member functions together to form an object.
  • Constructor– A Constructor is the same name of its class, will automatic invoke once the object of class in created.
  • Destructor– It refer to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.

Example:

<?php

// creating class called myClass

class myClass

{

public $i;

// creating function called myFn

function  myFn()

{

$c=10;

echo “Value is “.$c;

}

}

// Creating object for the class myClass

$ob=new myClass();

// Calling function through class object $ob

$ob->myFn();

?>

Constructor in PHP

<?php

class MyClass

{

public $prop1 = “I m a class variable”;

 

public function __construct()

{

echo ” This is a constructor line”;

}

 

public function __destruct()

{

echo ” bye bye..”;

}

 

public function setProperty($newval)

{

$this->prop1 = $newval;

}

 

public function getProperty()

{

return $this->prop1 . “<br />”;

}

}

 

// Create a new object

$obj = new MyClass();

echo “<br>”;

 

// Set the value of $prop1

$obj->setProperty(“Hello”);

 

// now call the new value of $prop1

echo $obj->getProperty();

?>

 

Example: Get / Set in PHP

<?php

class MyClass

{

public $prop1 = “I am class Variable”;

public function setProperty($newval)

{

$this->prop1 = $newval;

}

public function getProperty()

{

return $this->prop1 . “<br />”;

}

}

$obj = new MyClass;

// Get the property values

echo $obj->getProperty();

// Set a new value

$obj->setProperty(“I’m a new property value!”);

// let’s view the latest value

echo $obj->getProperty();

?>

 

Leave a Reply

Your email address will not be published.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/05/05/oops-concepts-php
YouTube60
YouTube