SlideShare uma empresa Scribd logo
1 de 52
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

Mais conteúdo relacionado

Mais procurados

Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design PatternsHugo Hamon
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in actionJace Ju
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium AppsNate 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 PimpleHugo 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-8PrinceGuru MS
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010Fabien 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 / XSolveXSolve
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill 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.3Fabien Potencier
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez 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 EditionNate 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 8XSolve
 
8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会Yusuke Ando
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of LithiumNate Abele
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Seri Moth
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & RESTHugo Hamon
 

Mais procurados (20)

Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design Patterns
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
 
Building Lithium Apps
Building Lithium AppsBuilding Lithium Apps
Building Lithium Apps
 
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
 
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)
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
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
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
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
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
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
 
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
 
Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
 
8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会8時間耐久CakePHP2 勉強会
8時間耐久CakePHP2 勉強会
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of Lithium
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
 

Semelhante a Oops in php

SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutesBarang CK
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 MinutesAzim 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.4Jeff 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 UnConRafael 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 OOPWildan Maulana
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsSam Hennessy
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designJean Michel
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overviewjsmith92
 
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 itRafael 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.5Leonardo Proietti
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat 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 clienteLeonardo Proietti
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 

Semelhante a 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
 
laravel tricks in 50minutes
laravel tricks in 50minuteslaravel tricks in 50minutes
laravel tricks in 50minutes
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
PHP OOP
PHP OOPPHP OOP
PHP OOP
 
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
 
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
 
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
 
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy ApplicationsAdding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
 
PHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くために
 
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
 
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
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
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
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 

Último

Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 

Último (20)

Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 

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”;