SlideShare a Scribd company logo
1 of 33
OOP with PHP
Michael Peacock
Whats in store

OOP Introduction
Classes and Objects
Encapsulation
Polymorphism
Inheritance
OOP introduction
Introduction to OOP

Concepts are represented as objects
Objects have properties which contain
information about the object
Functions associated with objects are called
methods
Classes and objects
Classes and Objects
A class is a definition of an object. It is a file
containing:
  The namespace of the class
  The name of the class
  A list of the properties of the class
  Implementations of any methods
A class becomes an object when it is
instantiated with the new keyword
Defining a class

<?php
class MyClass {

    // implementation of the class goes in here

}
Visibility
 Public refers to methods and properties which can
 be accessed or called from both within the object
 itself and outside of the object (e.g. other files and
 objects)
 Private refers to methods and properties which can
 only be accessed or called from within the object
 itself
 Protected refers to methods and properties which
 can be accessed or called from within the object
 itself and objects which extend from the object (child
 objects)
Methods and Properties
<?php
class MyClass {

    private $someProperty = 'a default value';
    private $anotherPropert;

    public function myMethod()
    {
        echo "This is my method being run";
    }

}
Magic Methods

   These are methods which are called
  automatically when certain actions are
     undertaken, though they can be
          manually called too.
  Typically prefixed with two underscores
Magic methods
__construct the constructor method; called on an object as soon
as it is instantiated. It can accept parameters.
__destruct the destructor method; called when an object stops
being used/referenced or during shutdown sequence
__toString is called when you try to use an object as a string (e.g.
echo $my_object) and returns the string representation of the
object
__clone is called when you try and clone an object (e.g.
$new_object = clone $old_object) useful for dereferencing any
unique IDs, etc.
Full list: http://php.net/manual/en/language.oop5.magic.php
With a constructor
<?php
class MyClass {

      private $someProperty = 'a default value';
      private $anotherPropert;

      public function __construct()
      {
            echo "this is called when you create the object";
      }

      public function myMethod()
      {
            echo "This is my method being run";
      }

}
Working with objects
Creating
$my_object = new SomeObject();
Accessing properties
echo $my_object->someProperty; // must be
public
Calling methods
$my_object->someMethods(); // must be
public
$this
When working within an object, you access
properties and methods within the object using
the $this keyword.
For example, consider a method which returns
the total cost for an order. This needs to add the
cost and the delivery cost. Both of these costs
are calculated using other methods

return $this->calculateCost() + $this->calculateDeliveryCost();
Static
    Properties can be accessed and
     methods can be called from an
uninstantiated class (i.e. not an object) if
     they are prefixed with the static
keyword. They are called and accessed
 with the Scope Resolution Operator (::)
 class MyClass {
        public static function printHello()
        {
        print “hello”;
        }
 }

 MyClass::printHello();
Class constants

Classes can also contain constants; these are
similar to properties except they cannot be
changed (unless you edit the php file of
course!)
const myConstant = “some value”;
Interfaces and
Implements
An interface defines non-private methods (as
well as the number of parameters they accept)
that a class which implements the interface
must have
If a class specifically implements an interface it
must implement the methods defined, if it does
not PHP will raise errors.
Defining an interface

<?php
interface MyInterface {

     public function mustImplementThis();

      public function mustImplementThisAsWell($with, $some,
$parameters=null);


}
Creating a class which
    implements an interface
<?php
class MyClass implements MyInterface {

     public function mustImplementThis()
     {
          // put some code here
     }

     public function mustImplementThisAsWell($with, $some,
$parameters=null)
     {
           // put some code here
     }

}
Extends
One class can extend another. When it does
this the class inherits properties, methods and
constants from the parent class (the one it
extends) - this is where visibility settings are
essential.
Public and protected properties and methods
can be overridden in the child class; provided
the method names and number of parameters
match.
Extends in action
<?php
class MyChildClass extends MyParentClass {

     // now we have a class which has the same properties
     // and methods as the MyParentClass

     // we can add new ones here and override the parent ones
     // if we want to

}
Parent keyword

If you have a method in a child class from which
you want to access properties or methods in
the parent class, you use the parent keyword
with the scope resolution operator.

 <?php
 class Someclass extends Parentclass {

       public function test()
       {
              // this will call the someMethod method
              // in the Parentclass class
              echo parent::someMethod();
       }

 }
Abstract class
An abstract class gives us the best of both
worlds; we can define methods which need to
be implemented and we can create methods
and properties which can be extended by a
child class
An abstract class is defined using the abstract
keyword
However...an abstract class cannot be
instantiated directly. Only a class which extends
it can be instantiated.
Defining an abstract
class
 <?php
 abstract class MyAbstractClass {

       protected $someProperty;

       public function implementMe();
       public function implementMeToo();

       protected function someMethod()
       {
             echo 'a';
       }

       public function __toString()
       {
             return $this->someProperty;
       }

 }
Using an abstract class
<?php
class MyClass extends AbstractClass {

      protected $someProperty;

      public function implementMe()
      {
            // implementation
      }

      public function implementMeToo()
      {
            // implementation
      }

      // we dont need to implement someMethod() or __toString
      // as the abstract class implements them
      // we can override them if we want to

}
Encapsulation
Encapsulation
   With encapsulation the internal representation of an
object is hidden from view outside of the class. Often only
the object itself is permitted to directly access and modify
                        its properties.

  This approach means we, the programmer, can have
 greater control over how these properties are modified.

Imagine setting an email address property when a user fills
    out a contact form. If the property is public, then the
 property could be any value. If we hide the property and
   use a setter method, we can put some business logic
  between the user input and the property such as some
                          validation.
Polymorphism
Polymorphism

     Polymorphism allows an
    object, variable or function
    have more than one form.
Why polymorphism
You have a class (Email) which represents an email
You have a number of classes which can take an email
object and send it using a specific technology (e.g.
SMTP, sendmail, postmarkapp) lets use SmtpTransport
as an example
When you need to send an email you have some code
(emailer code) which takes the email, takes the
SmtpTransport and links the two together
If you want to use your other class,
PostmarkappTransport how does “email code” know
how to work with PostmarkappTransport...these are
both different classes
Why polymorphism...
Answer: Interfaces and polymorphism
We have a TransportInterface which defines the public methods
we will use to send the email
The *Transport classes all implement this and use their
implementations of the public methods to send the email
differently
Our email code doesn’t care about if its given a SmtpTransport or
PostmarkappTransport it only cares if they implement the
TransportInterface
Through polymorphism our Transport classes are both instances
of:
  *Transport
  TransportInterface
Type hinting to require
an interface

function myEmailCode(TransportInterface $transport, $email)
{
     $transport->setEmail($email);
     if( ! $transport->send() ) {
          echo $transport->getErrors();
     }
}

More Related Content

What's hot

Introduction to PHP OOP
Introduction to PHP OOPIntroduction to PHP OOP
Introduction to PHP OOPfakhrul hasan
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperNyros Technologies
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHPVibrant Technologies & Computers
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboardsDenis Ristic
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHPDavid Stockton
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Oop in-php
Oop in-phpOop in-php
Oop in-phpRajesh S
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpMichael Girouard
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
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
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .P
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 

What's hot (20)

Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Introduction to PHP OOP
Introduction to PHP OOPIntroduction to PHP OOP
Introduction to PHP OOP
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Php Oop
Php OopPhp Oop
Php Oop
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards09 Object Oriented Programming in PHP #burningkeyboards
09 Object Oriented Programming in PHP #burningkeyboards
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
PHP- Introduction to Object Oriented PHP
PHP-  Introduction to Object Oriented PHPPHP-  Introduction to Object Oriented PHP
PHP- Introduction to Object Oriented PHP
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHP
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
 
Only oop
Only oopOnly oop
Only oop
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
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
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
Variables in python
Variables in pythonVariables in python
Variables in python
 

Viewers also liked

Intermediate oop in php
Intermediate oop in phpIntermediate oop in php
Intermediate oop in phpDavid Stockton
 
Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)Mizan Riqzia
 
Restful api design
Restful api designRestful api design
Restful api designMizan Riqzia
 
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3harisonmtd
 
Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)Mizan Riqzia
 
OOP Basic - PHP
OOP Basic - PHPOOP Basic - PHP
OOP Basic - PHPSulaeman .
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017Chris Tankersley
 
PHP, Java EE & .NET Comparison
PHP, Java EE & .NET ComparisonPHP, Java EE & .NET Comparison
PHP, Java EE & .NET ComparisonHaim Michael
 
Comparison of Programming Platforms
Comparison of Programming PlatformsComparison of Programming Platforms
Comparison of Programming PlatformsAnup Hariharan Nair
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in phpherat university
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedSlideShare
 
What Makes Great Infographics
What Makes Great InfographicsWhat Makes Great Infographics
What Makes Great InfographicsSlideShare
 

Viewers also liked (20)

Beginning OOP in PHP
Beginning OOP in PHPBeginning OOP in PHP
Beginning OOP in PHP
 
Intermediate oop in php
Intermediate oop in phpIntermediate oop in php
Intermediate oop in php
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
 
Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)Mari Memahami PSR (PHP Standards Recommendation)
Mari Memahami PSR (PHP Standards Recommendation)
 
Restful api design
Restful api designRestful api design
Restful api design
 
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
 
Tutorial dasar laravel
Tutorial dasar laravelTutorial dasar laravel
Tutorial dasar laravel
 
Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)Nge-GIT (Belajar Git Bareng)
Nge-GIT (Belajar Git Bareng)
 
PHP versus Java
PHP versus JavaPHP versus Java
PHP versus Java
 
OOP Basic - PHP
OOP Basic - PHPOOP Basic - PHP
OOP Basic - PHP
 
Fungsi-Fungsi PHP
Fungsi-Fungsi PHPFungsi-Fungsi PHP
Fungsi-Fungsi PHP
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017
 
PHP, Java EE & .NET Comparison
PHP, Java EE & .NET ComparisonPHP, Java EE & .NET Comparison
PHP, Java EE & .NET Comparison
 
Comparison of Programming Platforms
Comparison of Programming PlatformsComparison of Programming Platforms
Comparison of Programming Platforms
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in php
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
LinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-PresentedLinkedIn SlideShare: Knowledge, Well-Presented
LinkedIn SlideShare: Knowledge, Well-Presented
 
What Makes Great Infographics
What Makes Great InfographicsWhat Makes Great Infographics
What Makes Great Infographics
 

Similar to Introduction to OOP with PHP

Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfGammingWorld2
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptxrani marri
 
Application package
Application packageApplication package
Application packageJAYAARC
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
Object oriented programming in php
Object oriented programming in phpObject oriented programming in php
Object oriented programming in phpAashiq Kuchey
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsMaryo Manjaruni
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5Sayed Ahmed
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxDavidLazar17
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Alena Holligan
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Alena Holligan
 

Similar to Introduction to OOP with PHP (20)

Object_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdfObject_oriented_programming_OOP_with_PHP.pdf
Object_oriented_programming_OOP_with_PHP.pdf
 
OOPS IN PHP.pptx
OOPS IN PHP.pptxOOPS IN PHP.pptx
OOPS IN PHP.pptx
 
Application package
Application packageApplication package
Application package
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Object oriented programming in php
Object oriented programming in phpObject oriented programming in php
Object oriented programming in php
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Oops
OopsOops
Oops
 
Classes2
Classes2Classes2
Classes2
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017Demystifying Object-Oriented Programming - PHP[tek] 2017
Demystifying Object-Oriented Programming - PHP[tek] 2017
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
 

More from Michael Peacock

Immutable Infrastructure with Packer Ansible and Terraform
Immutable Infrastructure with Packer Ansible and TerraformImmutable Infrastructure with Packer Ansible and Terraform
Immutable Infrastructure with Packer Ansible and TerraformMichael Peacock
 
Test driven APIs with Laravel
Test driven APIs with LaravelTest driven APIs with Laravel
Test driven APIs with LaravelMichael Peacock
 
Symfony Workflow Component - Introductory Lightning Talk
Symfony Workflow Component - Introductory Lightning TalkSymfony Workflow Component - Introductory Lightning Talk
Symfony Workflow Component - Introductory Lightning TalkMichael Peacock
 
Alexa, lets make a skill
Alexa, lets make a skillAlexa, lets make a skill
Alexa, lets make a skillMichael Peacock
 
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with LaravelMichael Peacock
 
An introduction to Laravel Passport
An introduction to Laravel PassportAn introduction to Laravel Passport
An introduction to Laravel PassportMichael Peacock
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony componentsMichael Peacock
 
Dance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech TalkDance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech TalkMichael Peacock
 
Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Michael Peacock
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsMichael Peacock
 
Evolution of a big data project
Evolution of a big data projectEvolution of a big data project
Evolution of a big data projectMichael Peacock
 
Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012Michael Peacock
 
Dealing with Continuous Data Processing, ConFoo 2012
Dealing with Continuous Data Processing, ConFoo 2012Dealing with Continuous Data Processing, ConFoo 2012
Dealing with Continuous Data Processing, ConFoo 2012Michael Peacock
 
Data at Scale - Michael Peacock, Cloud Connect 2012
Data at Scale - Michael Peacock, Cloud Connect 2012Data at Scale - Michael Peacock, Cloud Connect 2012
Data at Scale - Michael Peacock, Cloud Connect 2012Michael Peacock
 
PHP Continuous Data Processing
PHP Continuous Data ProcessingPHP Continuous Data Processing
PHP Continuous Data ProcessingMichael Peacock
 
PHP North East Registry Pattern
PHP North East Registry PatternPHP North East Registry Pattern
PHP North East Registry PatternMichael Peacock
 

More from Michael Peacock (20)

Immutable Infrastructure with Packer Ansible and Terraform
Immutable Infrastructure with Packer Ansible and TerraformImmutable Infrastructure with Packer Ansible and Terraform
Immutable Infrastructure with Packer Ansible and Terraform
 
Test driven APIs with Laravel
Test driven APIs with LaravelTest driven APIs with Laravel
Test driven APIs with Laravel
 
Symfony Workflow Component - Introductory Lightning Talk
Symfony Workflow Component - Introductory Lightning TalkSymfony Workflow Component - Introductory Lightning Talk
Symfony Workflow Component - Introductory Lightning Talk
 
Alexa, lets make a skill
Alexa, lets make a skillAlexa, lets make a skill
Alexa, lets make a skill
 
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with Laravel
 
An introduction to Laravel Passport
An introduction to Laravel PassportAn introduction to Laravel Passport
An introduction to Laravel Passport
 
Phinx talk
Phinx talkPhinx talk
Phinx talk
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
 
Dance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech TalkDance for the puppet master: G6 Tech Talk
Dance for the puppet master: G6 Tech Talk
 
Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Powerful and flexible templates with Twig
Powerful and flexible templates with Twig
 
Vagrant
VagrantVagrant
Vagrant
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
 
Evolution of a big data project
Evolution of a big data projectEvolution of a big data project
Evolution of a big data project
 
Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012Real time voice call integration - Confoo 2012
Real time voice call integration - Confoo 2012
 
Dealing with Continuous Data Processing, ConFoo 2012
Dealing with Continuous Data Processing, ConFoo 2012Dealing with Continuous Data Processing, ConFoo 2012
Dealing with Continuous Data Processing, ConFoo 2012
 
Data at Scale - Michael Peacock, Cloud Connect 2012
Data at Scale - Michael Peacock, Cloud Connect 2012Data at Scale - Michael Peacock, Cloud Connect 2012
Data at Scale - Michael Peacock, Cloud Connect 2012
 
Supermondays twilio
Supermondays twilioSupermondays twilio
Supermondays twilio
 
PHP & Twilio
PHP & TwilioPHP & Twilio
PHP & Twilio
 
PHP Continuous Data Processing
PHP Continuous Data ProcessingPHP Continuous Data Processing
PHP Continuous Data Processing
 
PHP North East Registry Pattern
PHP North East Registry PatternPHP North East Registry Pattern
PHP North East Registry Pattern
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 

Introduction to OOP with PHP

  • 2. Whats in store OOP Introduction Classes and Objects Encapsulation Polymorphism Inheritance
  • 4. Introduction to OOP Concepts are represented as objects Objects have properties which contain information about the object Functions associated with objects are called methods
  • 6. Classes and Objects A class is a definition of an object. It is a file containing: The namespace of the class The name of the class A list of the properties of the class Implementations of any methods A class becomes an object when it is instantiated with the new keyword
  • 7. Defining a class <?php class MyClass { // implementation of the class goes in here }
  • 8. Visibility Public refers to methods and properties which can be accessed or called from both within the object itself and outside of the object (e.g. other files and objects) Private refers to methods and properties which can only be accessed or called from within the object itself Protected refers to methods and properties which can be accessed or called from within the object itself and objects which extend from the object (child objects)
  • 9. Methods and Properties <?php class MyClass { private $someProperty = 'a default value'; private $anotherPropert; public function myMethod() { echo "This is my method being run"; } }
  • 10. Magic Methods These are methods which are called automatically when certain actions are undertaken, though they can be manually called too. Typically prefixed with two underscores
  • 11. Magic methods __construct the constructor method; called on an object as soon as it is instantiated. It can accept parameters. __destruct the destructor method; called when an object stops being used/referenced or during shutdown sequence __toString is called when you try to use an object as a string (e.g. echo $my_object) and returns the string representation of the object __clone is called when you try and clone an object (e.g. $new_object = clone $old_object) useful for dereferencing any unique IDs, etc. Full list: http://php.net/manual/en/language.oop5.magic.php
  • 12. With a constructor <?php class MyClass { private $someProperty = 'a default value'; private $anotherPropert; public function __construct() { echo "this is called when you create the object"; } public function myMethod() { echo "This is my method being run"; } }
  • 13. Working with objects Creating $my_object = new SomeObject(); Accessing properties echo $my_object->someProperty; // must be public Calling methods $my_object->someMethods(); // must be public
  • 14. $this When working within an object, you access properties and methods within the object using the $this keyword. For example, consider a method which returns the total cost for an order. This needs to add the cost and the delivery cost. Both of these costs are calculated using other methods return $this->calculateCost() + $this->calculateDeliveryCost();
  • 15. Static Properties can be accessed and methods can be called from an uninstantiated class (i.e. not an object) if they are prefixed with the static keyword. They are called and accessed with the Scope Resolution Operator (::) class MyClass { public static function printHello() { print “hello”; } } MyClass::printHello();
  • 16. Class constants Classes can also contain constants; these are similar to properties except they cannot be changed (unless you edit the php file of course!) const myConstant = “some value”;
  • 17. Interfaces and Implements An interface defines non-private methods (as well as the number of parameters they accept) that a class which implements the interface must have If a class specifically implements an interface it must implement the methods defined, if it does not PHP will raise errors.
  • 18. Defining an interface <?php interface MyInterface { public function mustImplementThis(); public function mustImplementThisAsWell($with, $some, $parameters=null); }
  • 19. Creating a class which implements an interface <?php class MyClass implements MyInterface { public function mustImplementThis() { // put some code here } public function mustImplementThisAsWell($with, $some, $parameters=null) { // put some code here } }
  • 20. Extends One class can extend another. When it does this the class inherits properties, methods and constants from the parent class (the one it extends) - this is where visibility settings are essential. Public and protected properties and methods can be overridden in the child class; provided the method names and number of parameters match.
  • 21. Extends in action <?php class MyChildClass extends MyParentClass { // now we have a class which has the same properties // and methods as the MyParentClass // we can add new ones here and override the parent ones // if we want to }
  • 22. Parent keyword If you have a method in a child class from which you want to access properties or methods in the parent class, you use the parent keyword with the scope resolution operator. <?php class Someclass extends Parentclass { public function test() { // this will call the someMethod method // in the Parentclass class echo parent::someMethod(); } }
  • 23. Abstract class An abstract class gives us the best of both worlds; we can define methods which need to be implemented and we can create methods and properties which can be extended by a child class An abstract class is defined using the abstract keyword However...an abstract class cannot be instantiated directly. Only a class which extends it can be instantiated.
  • 24. Defining an abstract class <?php abstract class MyAbstractClass { protected $someProperty; public function implementMe(); public function implementMeToo(); protected function someMethod() { echo 'a'; } public function __toString() { return $this->someProperty; } }
  • 25. Using an abstract class <?php class MyClass extends AbstractClass { protected $someProperty; public function implementMe() { // implementation } public function implementMeToo() { // implementation } // we dont need to implement someMethod() or __toString // as the abstract class implements them // we can override them if we want to }
  • 27. Encapsulation With encapsulation the internal representation of an object is hidden from view outside of the class. Often only the object itself is permitted to directly access and modify its properties. This approach means we, the programmer, can have greater control over how these properties are modified. Imagine setting an email address property when a user fills out a contact form. If the property is public, then the property could be any value. If we hide the property and use a setter method, we can put some business logic between the user input and the property such as some validation.
  • 29.
  • 30. Polymorphism Polymorphism allows an object, variable or function have more than one form.
  • 31. Why polymorphism You have a class (Email) which represents an email You have a number of classes which can take an email object and send it using a specific technology (e.g. SMTP, sendmail, postmarkapp) lets use SmtpTransport as an example When you need to send an email you have some code (emailer code) which takes the email, takes the SmtpTransport and links the two together If you want to use your other class, PostmarkappTransport how does “email code” know how to work with PostmarkappTransport...these are both different classes
  • 32. Why polymorphism... Answer: Interfaces and polymorphism We have a TransportInterface which defines the public methods we will use to send the email The *Transport classes all implement this and use their implementations of the public methods to send the email differently Our email code doesn’t care about if its given a SmtpTransport or PostmarkappTransport it only cares if they implement the TransportInterface Through polymorphism our Transport classes are both instances of: *Transport TransportInterface
  • 33. Type hinting to require an interface function myEmailCode(TransportInterface $transport, $email) { $transport->setEmail($email); if( ! $transport->send() ) { echo $transport->getErrors(); } }