SlideShare a Scribd company logo
B Y G O U R I S H A N K A R R P U J A R
OOPS in PHP
Introduction
 It Provides Modular Structure for your application
 It Makes Easy to maintain Existing Code.
 Here we will be creating Class file & Index file.
 Class file will be filled with Class, Objects, Functions.
 Where as Index file Just Shows the result of that class or
Function when it is called.
List of Data types
 Booleans
 Integers
 Floating Point Numbers
 Strings
 Arrays
 Objects
 Resources – File Handle
 Null
 Call-backs
Objects
 $object = new stdClass;
 $object->name = “Your Name”;
 Echo $object->name;
Index
 Inheritance
 Visibility
 Dependency Injection
 Interface
 Magic Methods
 Abstract
 Static
 Method Chaining
 Auto loading
How to use a Class
$object = new stdClass;
$object->names = [‘BMW’, ‘Audi’, ‘Benz’, ‘Jeep’];
foreach($object->names as $name){
Echo $name . “<br>”;
}
Example
 Student.php
Class Student{
public $name;
public $rollno;
}
Index.php
Require(‘student.php’);
$student = new Student;
$student->name = “Gourish”;
$student->rollno = “7”;
Echo $student->name .‘ has a roll no of ’. $student->rollno;
Using Method
 Student.php
Class Student{
public $name;
public $rollno;
public function sentence(){
return $this->name .‘ has a roll no of ’. $this->rollno;
}
}
Index.php
Require(‘student.php’);
$student = new Student;
$student->name = “Gourish”;
$student->rollno = 7;
Echo $student->sentence();
Contructors
 This is also called as magic method
 It has 2 Underscores.
 This will be constructed when a class is loaded.
 Public function __Construct(){
echo “Constructed”;
}
Example
 Student.php
Class Student{
public $name;
public $rollno;
public function __construct($name, $rollno){
$this->name = $name;
$this->rollno = $rollno;
}
public function sentence(){
return $this->name .‘ has a roll no of ’. $this->rollno;
}
}
Index.php
Require(‘student.php’);
$student = new Student(“Gourish”, 7);
Echo $student->sentence();
Inheritance (including)
 Bird.php
Class Bird{
public $canFly;
public $legCount;
public function __construct($canFly, $legCount){
$this->canfly = $canFly;
$this->legCount = $legCount;
}
public function canFly(){
return $this->canFly;
}
public function getlegCount (){
return $this-> legCount;
}
}
 Index.php
Require ”Bird.php”;
$bird = new Bird(true, 2);
Echo $bird->getLegCount();
 Pigeon.php
Class Pigeon extends Bird{
}
 Index.php
Require “Bird.php”
Require “Pigeon.php”;
$pigeon = new Pigeon(true, 2);
Echo $pigeon->getLegCount();
If($pigeon->$canFly()){
echo “Can Fly”;
}
 Can you try it for Penguin
Visibility
 Three access / visibility modifiers introduced in PHP 5, which
affect the scope of access to class variables and functions:
 public : public class variables and functions can be accessed from inside and
outside the class
 protected : hides a variable or function from direct external class access +
protected members are available in subclasses
 private : hides a variable or function from direct external class access +
protected members are hidden (NOT available) from all subclasses
 An access modifier has to be provided for each class instance
variable
 Static class variables and functions can be declared without an
access modifier → default is public
 Penguin.php
Change objects in bird class to Protected.
Class Penguin extends Bird{
public function foo(){
echo $legCount(); // This is picking up from protected object
}
}
Dependency Injection
 Till Now we have understood Inheritance
 But we don’t know how to utilize to its full potential.
 What is Dependency ?
 Create 3 files.
 Index.php
 Chest.php
 Lock.php
 Chest.php
Class Chest{
protected $lock;
protected $isClosed;
public function __construct($lock) {
$this->lock = true;
}
public function close($lock = true) {
if ($lock === true){
$this->lock->lock(); }
$this->isClosed = true;
echo “Closed”;
}
public function open() {
if ($this->lock->isLocked()){
$this->lock ->unlock(); }
$this->isClosed = false;
echo “Open”;
}
public function isClosed(){
return $this->isClosed;
}
}
 Lock.php
Class Lock {
protected $isLocked;
public function lock(){
$this->isLocked = true;
}
public fuction unlock() {
$this->isLocked = false;
}
public function isLocked(){
return $this->isLocked;
}
}
 Index.php
Require ‘chest.php’;
Require ‘lock.php’;
$chest = new Chest(new Lock);
$chest->close();
$chest->open();
Real World Example
 Index Page
 Database
 User
 Database Page
Class Database {
public function query($sql){
// $this->pdo->prepare($sql)->execute();
echo $sql;
}
}
 User.php
Class User {
protected $db;
public function __construct(Database $db){
$this->db = $db;
}
public function create(array $data){
$this->db->query(‘INSERT INTO ‘users’ … ’);
}
}
 Index.php
Require ‘Database.php’;
Require ‘User.php’;
$user = new User(new Database);
$user->create([‘username‘=>’gourish7’]);
Interfaces
 What is Interface ?
 Blueprint for a class.
 3 Methods of file representation
1. Itest.php
2. I_Test.php
3. TestInterface.php
Example 1
 Collection.php
Class Collection {
protected $items = [];
public function add ($value){
$this->items[] = $value;
}
public function set($key, $value){
$this->items[‘$key’] = $value;
}
public function toJson(){
return json_encode($this->items);
}
}
 Index.php
Require ‘Collection.php’;
$c = new Collection()
$c->add(‘name1’);
$c->add(‘name2’);
Echo $c->toJson();
Echo count($c);
Example 2
 TalkInterface.php
Interface Talk{
public function talk();
}
 Company.php
Class Company implements TalkInterface{
public function talk(){
return ‘Welcome Sir/Madam, How can I
help you today’;
}
}
 Person.php
Class Person implements TalkInterface{
public function talk(){
return ‘I need help in Resetting my
Phone’;
}
}
 Index.php
Require ‘TalkInterface.php’;
Require ‘Company.php’;
Require ‘Person.php’;
$company = new Company();
Echo $company->talk();
$person = new Person();
Echo $ person ->talk();
Magic Methods
 What is Magic Method ?
 __construct()
 __set()
 __get()
 __call()
 __toString()
SET Method
 Public function __set($key, $value) {
 $this->set($key, $value);
}
Public function set($key, $value){
$this->items[$key] = $value;
}
Public function all(){
return $this->items;
}
Index.php
$c->align = ‘center’;
Echo ‘pre’, print_r($c ->all());
Get
 Public function __get($value) {
 Return $this-> get(,$value);
}
Public function get($key){
retutn array_key_exists ($key, $this->items) ? $this->items[$key] : null;
}
Index.php
$c->align = ‘center’;
Echo $c->get(‘align’);
Echo $c->align;
Call
 Public function __call($func, $args) {
echo $func.’ has been called with arguments
‘.implode(‘, ’. $args);
}
Index.php
$c->align = ‘center’;
Echo $c->align(‘left’, ‘right’, ‘center’, ‘top’, ‘bottom’);
toString
 Public function __toString(){
 Return $this->jsonSerialize();
}
Public function jsonSerialize(){
return json_encode($this->items);
}
$c->add(‘foo’);
$c->add(‘bar’);
Echo $c;
Abstract
 What is Abstract ?
 It is an interface in which we can define default
implemetations.
Example
 User.php
Abstract Class User{
public function userdetails(){
return ‘Gourishankar’;
}
abstract public function userLocation();
}
 Address.php
Class Address extends User{
public function userAddress(){
return ‘Bengaluru’;
}
public function userLocation(){
return ‘Magadi Road’;
}
}
 Index.php
Require ‘User.php’;
Require ‘Address.php’;
$bar = new Bar;
Echo $bar->userdetails();
Echo $bar->userAddress();
Echo $bar->userLocation();
Static
 What is static ?
 Secret PHP Instance that is dedicated to static
methods & Properties.
 Use only when necessery
 Student.php
Class Student {
public static function name(){
return ‘Gourishankar R Pujar’;
}
}
 Index.php
Require ‘Student.php’;
Echo Student::name();
Method Chaining
 What is Method chaining ?
Example
 Cart.php
Class Cart{
public function order(){
echo “Order <br>”;
return $this;
}
public function book(){
echo “Book”;
}
}
 Index.php
Require “cart.php”;
$cart = new Cart();
$cart->order()->book();
Autoloading
 What is Autoloading ?
 Example – We have to include 10 files in index file
then how we do ?
Method 1
Init.php Index.php
Require_once “Classes/Student.php”;
Require_once “Classes/Parent.php”;
Require_once “Classes/Teacher.php”;
Require_once “Classes/Admin.php”;
Require_once “Classes/Account.php”;
Require_once “init.php”;
Method 2
Init.php Index.php
Spl_autoload_register(function($class)
{
require “class/{$class}.php”;
});
Require_once “init.php”;
Project
 Calculator
 Student Management
Oops in php
Ad

More Related Content

What's hot (20)

Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design Patterns
Hugo Hamon
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
Jace Ju
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
Nate Abele
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
Hugo Hamon
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Fabien Potencier
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
PrinceGuru MS
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
Fabien Potencier
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
Fabien Potencier
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
Bill Chang
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
Wez Furlong
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Nate Abele
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8
XSolve
 
Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
José Lorenzo Rodríguez Urdaneta
 
8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会
Yusuke Ando
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of Lithium
Nate Abele
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
Bastian Feder
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
Hugo Hamon
 
Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design Patterns
Hugo Hamon
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
Jace Ju
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
Nate Abele
 
Design Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et PimpleDesign Patterns avec PHP 5.3, Symfony et Pimple
Design Patterns avec PHP 5.3, Symfony et Pimple
Hugo Hamon
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Fabien Potencier
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
PrinceGuru MS
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
Fabien Potencier
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
Bill Chang
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
Wez Furlong
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Nate Abele
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8
XSolve
 
8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会
Yusuke Ando
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of Lithium
Nate Abele
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
Bastian Feder
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
Hugo Hamon
 

Similar to Oops in php (20)

SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
jsmith92
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
Barang CK
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
Azim Kurt
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
switipatel4
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 
PHP OOP
PHP OOPPHP OOP
PHP OOP
Oscar Merida
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
Rafael Dohms
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Sam Hennessy
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
Bradley Holt
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
Jean Michel
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
Yuya Takeyama
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
Rafael Dohms
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
Leonardo Proietti
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Rifat Nabi
 
Symfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il clienteSymfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il cliente
Leonardo Proietti
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
Daniel Knell
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
jsmith92
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
Barang CK
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
Azim Kurt
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
Jeff Carouth
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
Rafael Dohms
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Sam Hennessy
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
Jean Michel
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
jsmith92
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
Yuya Takeyama
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
Rafael Dohms
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
Leonardo Proietti
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Rifat Nabi
 
Symfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il clienteSymfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il cliente
Leonardo Proietti
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
Daniel Knell
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Ad

Recently uploaded (20)

π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Ad

Oops in php

  • 1. B Y G O U R I S H A N K A R R P U J A R OOPS in PHP
  • 2. Introduction  It Provides Modular Structure for your application  It Makes Easy to maintain Existing Code.  Here we will be creating Class file & Index file.  Class file will be filled with Class, Objects, Functions.  Where as Index file Just Shows the result of that class or Function when it is called.
  • 3. List of Data types  Booleans  Integers  Floating Point Numbers  Strings  Arrays  Objects  Resources – File Handle  Null  Call-backs
  • 4. Objects  $object = new stdClass;  $object->name = “Your Name”;  Echo $object->name;
  • 5. Index  Inheritance  Visibility  Dependency Injection  Interface  Magic Methods  Abstract  Static  Method Chaining  Auto loading
  • 6. How to use a Class $object = new stdClass; $object->names = [‘BMW’, ‘Audi’, ‘Benz’, ‘Jeep’]; foreach($object->names as $name){ Echo $name . “<br>”; }
  • 7. Example  Student.php Class Student{ public $name; public $rollno; } Index.php Require(‘student.php’); $student = new Student; $student->name = “Gourish”; $student->rollno = “7”; Echo $student->name .‘ has a roll no of ’. $student->rollno;
  • 8. Using Method  Student.php Class Student{ public $name; public $rollno; public function sentence(){ return $this->name .‘ has a roll no of ’. $this->rollno; } } Index.php Require(‘student.php’); $student = new Student; $student->name = “Gourish”; $student->rollno = 7; Echo $student->sentence();
  • 9. Contructors  This is also called as magic method  It has 2 Underscores.  This will be constructed when a class is loaded.  Public function __Construct(){ echo “Constructed”; }
  • 10. Example  Student.php Class Student{ public $name; public $rollno; public function __construct($name, $rollno){ $this->name = $name; $this->rollno = $rollno; } public function sentence(){ return $this->name .‘ has a roll no of ’. $this->rollno; } } Index.php Require(‘student.php’); $student = new Student(“Gourish”, 7); Echo $student->sentence();
  • 11. Inheritance (including)  Bird.php Class Bird{ public $canFly; public $legCount; public function __construct($canFly, $legCount){ $this->canfly = $canFly; $this->legCount = $legCount; } public function canFly(){ return $this->canFly; } public function getlegCount (){ return $this-> legCount; } }
  • 12.  Index.php Require ”Bird.php”; $bird = new Bird(true, 2); Echo $bird->getLegCount();
  • 13.  Pigeon.php Class Pigeon extends Bird{ }
  • 14.  Index.php Require “Bird.php” Require “Pigeon.php”; $pigeon = new Pigeon(true, 2); Echo $pigeon->getLegCount(); If($pigeon->$canFly()){ echo “Can Fly”; }
  • 15.  Can you try it for Penguin
  • 16. Visibility  Three access / visibility modifiers introduced in PHP 5, which affect the scope of access to class variables and functions:  public : public class variables and functions can be accessed from inside and outside the class  protected : hides a variable or function from direct external class access + protected members are available in subclasses  private : hides a variable or function from direct external class access + protected members are hidden (NOT available) from all subclasses  An access modifier has to be provided for each class instance variable  Static class variables and functions can be declared without an access modifier → default is public
  • 17.  Penguin.php Change objects in bird class to Protected. Class Penguin extends Bird{ public function foo(){ echo $legCount(); // This is picking up from protected object } }
  • 18. Dependency Injection  Till Now we have understood Inheritance  But we don’t know how to utilize to its full potential.  What is Dependency ?  Create 3 files.  Index.php  Chest.php  Lock.php
  • 19.  Chest.php Class Chest{ protected $lock; protected $isClosed; public function __construct($lock) { $this->lock = true; } public function close($lock = true) { if ($lock === true){ $this->lock->lock(); } $this->isClosed = true; echo “Closed”; } public function open() { if ($this->lock->isLocked()){ $this->lock ->unlock(); } $this->isClosed = false; echo “Open”; } public function isClosed(){ return $this->isClosed; } }
  • 20.  Lock.php Class Lock { protected $isLocked; public function lock(){ $this->isLocked = true; } public fuction unlock() { $this->isLocked = false; } public function isLocked(){ return $this->isLocked; } }
  • 21.  Index.php Require ‘chest.php’; Require ‘lock.php’; $chest = new Chest(new Lock); $chest->close(); $chest->open();
  • 22. Real World Example  Index Page  Database  User
  • 23.  Database Page Class Database { public function query($sql){ // $this->pdo->prepare($sql)->execute(); echo $sql; } }
  • 24.  User.php Class User { protected $db; public function __construct(Database $db){ $this->db = $db; } public function create(array $data){ $this->db->query(‘INSERT INTO ‘users’ … ’); } }
  • 25.  Index.php Require ‘Database.php’; Require ‘User.php’; $user = new User(new Database); $user->create([‘username‘=>’gourish7’]);
  • 26. Interfaces  What is Interface ?  Blueprint for a class.  3 Methods of file representation 1. Itest.php 2. I_Test.php 3. TestInterface.php
  • 27. Example 1  Collection.php Class Collection { protected $items = []; public function add ($value){ $this->items[] = $value; } public function set($key, $value){ $this->items[‘$key’] = $value; } public function toJson(){ return json_encode($this->items); } }
  • 28.  Index.php Require ‘Collection.php’; $c = new Collection() $c->add(‘name1’); $c->add(‘name2’); Echo $c->toJson(); Echo count($c);
  • 29. Example 2  TalkInterface.php Interface Talk{ public function talk(); }
  • 30.  Company.php Class Company implements TalkInterface{ public function talk(){ return ‘Welcome Sir/Madam, How can I help you today’; } }
  • 31.  Person.php Class Person implements TalkInterface{ public function talk(){ return ‘I need help in Resetting my Phone’; } }
  • 32.  Index.php Require ‘TalkInterface.php’; Require ‘Company.php’; Require ‘Person.php’; $company = new Company(); Echo $company->talk(); $person = new Person(); Echo $ person ->talk();
  • 33. Magic Methods  What is Magic Method ?  __construct()  __set()  __get()  __call()  __toString()
  • 34. SET Method  Public function __set($key, $value) {  $this->set($key, $value); } Public function set($key, $value){ $this->items[$key] = $value; } Public function all(){ return $this->items; } Index.php $c->align = ‘center’; Echo ‘pre’, print_r($c ->all());
  • 35. Get  Public function __get($value) {  Return $this-> get(,$value); } Public function get($key){ retutn array_key_exists ($key, $this->items) ? $this->items[$key] : null; } Index.php $c->align = ‘center’; Echo $c->get(‘align’); Echo $c->align;
  • 36. Call  Public function __call($func, $args) { echo $func.’ has been called with arguments ‘.implode(‘, ’. $args); } Index.php $c->align = ‘center’; Echo $c->align(‘left’, ‘right’, ‘center’, ‘top’, ‘bottom’);
  • 37. toString  Public function __toString(){  Return $this->jsonSerialize(); } Public function jsonSerialize(){ return json_encode($this->items); } $c->add(‘foo’); $c->add(‘bar’); Echo $c;
  • 38. Abstract  What is Abstract ?  It is an interface in which we can define default implemetations.
  • 39. Example  User.php Abstract Class User{ public function userdetails(){ return ‘Gourishankar’; } abstract public function userLocation(); }
  • 40.  Address.php Class Address extends User{ public function userAddress(){ return ‘Bengaluru’; } public function userLocation(){ return ‘Magadi Road’; } }
  • 41.  Index.php Require ‘User.php’; Require ‘Address.php’; $bar = new Bar; Echo $bar->userdetails(); Echo $bar->userAddress(); Echo $bar->userLocation();
  • 42. Static  What is static ?  Secret PHP Instance that is dedicated to static methods & Properties.  Use only when necessery
  • 43.  Student.php Class Student { public static function name(){ return ‘Gourishankar R Pujar’; } }
  • 45. Method Chaining  What is Method chaining ?
  • 46. Example  Cart.php Class Cart{ public function order(){ echo “Order <br>”; return $this; } public function book(){ echo “Book”; } }
  • 47.  Index.php Require “cart.php”; $cart = new Cart(); $cart->order()->book();
  • 48. Autoloading  What is Autoloading ?  Example – We have to include 10 files in index file then how we do ?
  • 49. Method 1 Init.php Index.php Require_once “Classes/Student.php”; Require_once “Classes/Parent.php”; Require_once “Classes/Teacher.php”; Require_once “Classes/Admin.php”; Require_once “Classes/Account.php”; Require_once “init.php”;
  • 50. Method 2 Init.php Index.php Spl_autoload_register(function($class) { require “class/{$class}.php”; }); Require_once “init.php”;