SlideShare una empresa de Scribd logo
1 de 52
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Similar 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
 

Similar 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

Detection&Tracking - Thermal imaging object detection and tracking
Detection&Tracking - Thermal imaging object detection and trackingDetection&Tracking - Thermal imaging object detection and tracking
Detection&Tracking - Thermal imaging object detection and trackinghadarpinhas1
 
Uk-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Exp...
Uk-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Exp...Uk-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Exp...
Uk-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Exp...Amil baba
 
Javier_Fernandez_CARS_workshop_presentation.pptx
Javier_Fernandez_CARS_workshop_presentation.pptxJavier_Fernandez_CARS_workshop_presentation.pptx
Javier_Fernandez_CARS_workshop_presentation.pptxJavier Fernández Muñoz
 
KCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitosKCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitosVictor Morales
 
22CYT12 & Chemistry for Computer Systems_Unit-II-Corrosion & its Control Meth...
22CYT12 & Chemistry for Computer Systems_Unit-II-Corrosion & its Control Meth...22CYT12 & Chemistry for Computer Systems_Unit-II-Corrosion & its Control Meth...
22CYT12 & Chemistry for Computer Systems_Unit-II-Corrosion & its Control Meth...KrishnaveniKrishnara1
 
Guardians of E-Commerce: Harnessing NLP and Machine Learning Approaches for A...
Guardians of E-Commerce: Harnessing NLP and Machine Learning Approaches for A...Guardians of E-Commerce: Harnessing NLP and Machine Learning Approaches for A...
Guardians of E-Commerce: Harnessing NLP and Machine Learning Approaches for A...IJAEMSJORNAL
 
ADM100 Running Book for sap basis domain study
ADM100 Running Book for sap basis domain studyADM100 Running Book for sap basis domain study
ADM100 Running Book for sap basis domain studydhruvamdhruvil123
 
AntColonyOptimizationManetNetworkAODV.pptx
AntColonyOptimizationManetNetworkAODV.pptxAntColonyOptimizationManetNetworkAODV.pptx
AntColonyOptimizationManetNetworkAODV.pptxLina Kadam
 
Secure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech LabsSecure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech Labsamber724300
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxRomil Mishra
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 
tourism-management-srs_compress-software-engineering.pdf
tourism-management-srs_compress-software-engineering.pdftourism-management-srs_compress-software-engineering.pdf
tourism-management-srs_compress-software-engineering.pdfchess188chess188
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHSneha Padhiar
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsResearcher Researcher
 
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...gerogepatton
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Communityprachaibot
 

Último (20)

Versatile Engineering Construction Firms
Versatile Engineering Construction FirmsVersatile Engineering Construction Firms
Versatile Engineering Construction Firms
 
Detection&Tracking - Thermal imaging object detection and tracking
Detection&Tracking - Thermal imaging object detection and trackingDetection&Tracking - Thermal imaging object detection and tracking
Detection&Tracking - Thermal imaging object detection and tracking
 
ASME-B31.4-2019-estandar para diseño de ductos
ASME-B31.4-2019-estandar para diseño de ductosASME-B31.4-2019-estandar para diseño de ductos
ASME-B31.4-2019-estandar para diseño de ductos
 
Uk-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Exp...
Uk-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Exp...Uk-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Exp...
Uk-NO1 Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Exp...
 
Javier_Fernandez_CARS_workshop_presentation.pptx
Javier_Fernandez_CARS_workshop_presentation.pptxJavier_Fernandez_CARS_workshop_presentation.pptx
Javier_Fernandez_CARS_workshop_presentation.pptx
 
KCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitosKCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitos
 
22CYT12 & Chemistry for Computer Systems_Unit-II-Corrosion & its Control Meth...
22CYT12 & Chemistry for Computer Systems_Unit-II-Corrosion & its Control Meth...22CYT12 & Chemistry for Computer Systems_Unit-II-Corrosion & its Control Meth...
22CYT12 & Chemistry for Computer Systems_Unit-II-Corrosion & its Control Meth...
 
Guardians of E-Commerce: Harnessing NLP and Machine Learning Approaches for A...
Guardians of E-Commerce: Harnessing NLP and Machine Learning Approaches for A...Guardians of E-Commerce: Harnessing NLP and Machine Learning Approaches for A...
Guardians of E-Commerce: Harnessing NLP and Machine Learning Approaches for A...
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
ADM100 Running Book for sap basis domain study
ADM100 Running Book for sap basis domain studyADM100 Running Book for sap basis domain study
ADM100 Running Book for sap basis domain study
 
AntColonyOptimizationManetNetworkAODV.pptx
AntColonyOptimizationManetNetworkAODV.pptxAntColonyOptimizationManetNetworkAODV.pptx
AntColonyOptimizationManetNetworkAODV.pptx
 
Secure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech LabsSecure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech Labs
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 
tourism-management-srs_compress-software-engineering.pdf
tourism-management-srs_compress-software-engineering.pdftourism-management-srs_compress-software-engineering.pdf
tourism-management-srs_compress-software-engineering.pdf
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
 
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
 

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