SlideShare a Scribd company logo
OOP and PHP 5.3


    South Florida PHP Users Group


                       Adam Culp
                      http://www.Geekyboy.com



In this presentation we will talk about OOP, and discuss
  the object model progression in PHP from version 4
  through the newest 5.3.
I apologize in advance, but this will not be in great detail
   though I hope it is enough to help everyone in some
   way.
Procedural? Really?

• PHP was started as a procedural         <?php
                                          function vehicle($wheels, $color, $hp) {
  tool to perform quick tasks.                 // build a car using info provided
  Large adoption brought more                  $car = array(
  users who wanted OOP, so here                              ‘wheels’ => $wheels,
                                                             ‘color’ => $color,
  we are.                                                    ‘hp’ => $hp);
   – Everything went fairly sequential         return $car;
   – Took a fair amount of labor within   }

     code to change values afterward      $car = vehicle(4, ‘red’, 240);
   – Procedural does make sense for       // now continue with code that uses $car
     simple tasks, such as CRON jobs to   If ($car[‘color’] == ‘red’) {
     perform quick tasks.                       echo ‘Your car is red’;
                                          }

                                          ?>

                                          Output:
                                          Your car is red


                                          Possible usage:
                                          Here is the car: <?php print_r($car); ?>
OOP the basics

• PHP 4 brought the introduction            <?php
                                            Interface mobility {
                                                   function setColor($color);
  of oop to PHP.                            }
                                                   function setHorsepower($hp);

   – Application builds an object           class Vehicle implements mobility {
   – Helps with DRY (Don’t repeat                 public $color;
                                                  public $horsepower;
     yourself)                                    function setColor($color) {
   – Simplifies structure, code still can         }
                                                            $this->color = $color;

     be sequential but is more ‘modular’          function setHorsepower($hp) {
   – Helps keep code more readable                }
                                                            $this->horsepower = $hp;

                                            }
   – Object can be manipulated easier
     prior to use of the object             Class Car extends Vehicle {
                                                  public $wheel;

   – We instantiate the class by creating         function addWheel($n) {

     a “new” object, then manipulate the          }
                                                            $this->wheel += $n;

     object by calling the different        }

     “methods” within the class.            $myCar = new Car();
                                            $myCar->setColor(‘red’);
                                            $myCar->setHorsepower(250);
                                            $myCar->addWheel(3);
                                            $myCar->addWheel(1);

                                            print_r($myCar);
                                            ?>

                                            Output:
                                            Car Object([wheel]=>4[color]=>red[horsepower]=>250)
PHP 4 revisit

• PHP 4 had a very basic OOP                 <?php
                                             class Reference {
  presence, and looked something                  var $reference;
  like this ->
                                                  // acted as constructor
   – Variables need to be defined as              function Reference() {
     ‘var’.                                            $this->reference = ‘dictionary’;
   – Constructors carried the same name           }
     as the Class.
                                                  function getReference() {
       • Note: the constructor always runs             return $this->reference;
         “automagically” when the class is        }
         instantiated                        }
   – No visibility
                                             $rt = new Reference();
   – No abstraction                          $reference = $rt->getReference;
   – Very simple and easy to use, but        echo ‘reference:’ . $reference;
     lacked many features of other OOP       ?>
     languages at the time.
                                             Output:
                                             reference: dictionary
PHP 5.0 brought changes

•   With the rollout of PHP 5.0 there were many          <?php
                                                         class Reference {
    changes.                                                    const DEFAULT_LANG = ‘eng’;
     – Protected data with Visibility                          private $reference;
           • Public (default) – accessed and changed           private $lang;

              globally                                         public function __construct(Language $lang) {
                                                                         if($lang ) {
           • Protected – access and changed by                                    $this->lang = $lang->esp;
              direct descendants                                         } else {
                                                                                  $this->lang = DEFAULT_LANG;
           • Private – access and changed within                         }
              class only                                                 $this->reference = ‘dictionary’;
                                                               }
     – Type Hinting – notice how we specify that an
                                                               public function getReference() {
        object of Language is passed to the                              return $this->reference;
        constructor. This means we must create an              }
        object using the Language class first.
                                                               private function setPrivateReference() {
     – Variables no longer need the ‘var’ keyword                        $this->reference = ‘my_dictionary’;
                                                               }
     – Constructor now defined using __construct         }
        call
                                                         class Language {
     – CONSTANT values may now be assigned                      public $esp = ‘Spanish’;
        per-class, cannot be a variable or property or   }

        mathematical operation or function call.         $lang = new Language();
                                                         $rt = new Reference($lang);
                                                         $reference = $rt->getReference;
                                                         echo ‘reference:’ . $reference;
                                                         ?>

                                                         Output:
                                                         Reference: Spanish
PHP 5.0 Abstraction

•   Abstraction                                        <?php
                                                       abstract class Reference {
     – Abstraction, if a class contains any abstract           public $reference;

        methods the class must also be abstract.                  abstract public function read();
     – Abstracted methods must be defined by the                  public function __construct() {
        child class.                                                      $this->reference = ‘dictionary’;
                                                                  }
     – Visibility in the method of the child class
        must be the same, or less, restricted.                    final public function getReference() {
                                                                          return $this->reference;
     – “final” keyword prevents child classes from                }
        overriding a method
                                                                  protected function setProtectedReference($myReference) {
     – “clone” creates a copy of an object rather                         $this->reference = $myReference;
                                                                  }
        than continuing to use the same object.        }

                                                       class Letter extends Reference {
                                                              public function read($personal) {
                                                                        $myDictionary = $personal;

                                                                          parent::setProtectedReference($myDictionary);

                                                                          return $this->reference;
                                                              }
                                                       }

                                                       $rt = new Letter();
                                                       $reference = $rt->read(‘my english dictionary’);
                                                       $rt2 = clone $rt;

                                                       echo ‘reference:’ . $reference;
                                                       ?>

                                                       Output:
                                                       reference: my english dictionary
PHP 5.0 Interfaces

•   Interface                                     <?php
                                                  interface rTemplate
     – Interface, specifies which methods a       {
                                                        public function getReference();
         class must implement.                          public function setProtectedReference();
                                                  }
     – All methods in interface must be public.
     – Multiple interfaces can be implemented     class Reference implements rTemplate {
                                                         public $reference;
         by using comma separation
                                                          public function __construct() {
     – Interface may contain a CONSTANT,                          $this->reference = ‘dictionary’;
         but may not be overridden by                     }
         implementing class                               public function getReference() {
                                                                  return $this->reference;
                                                          }

                                                          protected function setProtectedReference($myReference) {
                                                                 $this->reference = $myReference;
                                                          }
                                                  }

                                                  $rt = new Letter();
                                                  $reference = $rt->getReference();

                                                  echo ‘reference:’ . $reference;
                                                  ?>

                                                  Output:
                                                  reference: dictionary
PHP 5.0 Exceptions

•                                              <?php
    Additional features with PHP 5.            function inverse($x) {
     – Exceptions – throwing an exception          if (!$x) {
                                                            throw new Exception(‘Division by zero.’);
       to gracefully error out, while              } else {
       continuing to execute the rest of the                return 1/$x;
       script even after the error.                }
                                               }
         • Notice how we still get ‘Hello
           World’ even after an exception      try {
           caused by ‘Division by zero’.            echo inverse(5) . “n”;
                                                    echo inverse(0) . “n”; // trigger exception
     – Exceptions can then be caught for       } catch (Exception $e) {
       logging, etc.                                echo ‘Caught exception: ‘, $e->getMessage(), “n”;
                                               }

                                               // continue execution
                                               echo ‘Hello World’;
                                               ?>

                                               Output:
                                               0.2
                                               Caught exception: Division by zero.
                                               Hello World
Finally, PHP 5.3 features

•   Additional features with PHP 5.3
     – Namespaces
     – Late Static Bindings
     – Jump labels (goto)
     – Closures
     – __callStatic() and __invoke()
     – Class Constants
     – Nowdoc syntax supported
     – Use Heredocs to initialize static
       variables and class properties
     – Heredocs with double quotes
     – Ternary operator shortcut
     – HTTP status 200 to 399 = success
     – Dynamic access to static methods
     – Exception nesting
     – mail() logging of sent email
Namespaces

•   Namespaces                                   <?php
                                                 declare (encoding=‘UTF-8’);
     – Help create a new layer of code           namespace automobile;
       encapsulation.                            class Automobile {
         • Keep properties from colliding              function setType($type) {
                                                                echo __NAMESPACE__ . “n”;
           between areas of your code                  }
         • Only classes, interfaces, functions   }

           and constants are affected            namespace automobilecar;
     – Anything that does not have a             class Car {
       namespace is considered in the                  function toyota() {
                                                                echo “test driven”;
       Global namespace (namespace =                   }
       “”)                                       }

     – Namespace must be declared first          $car = new Car
                                                 $car->toyota();
       (except ‘declare’ statement)
                                                 // OR you can use the namespace
     – Can define multiple namespaces in
                                                 $auto = new automobilecarCar;
       the same file.                            $auto->toyota();

                                                 ?>

                                                 Output:
                                                 test drive
                                                 test drive
Namespaces – cont.

•   Namespaces – cont.                          <?php
                                                namespace automobile;
     – You can define that something be         class Automobile {
       used in the “Global” namespace by              function setType($type) {
                                                        echo __NAMESPACE__ . “n”;
       enclosing a non-labeled namespace              }
       in {} brackets. (Note: if you have       }

       multiple namespaces in the same          namespace automobilecar;
       file they must all use this notation.)   use automobile as auto;
     – Use namespaces from within other         class Car {
       namespaces, along with aliasing                function toyota() {
                                                        echo “test driven”;
                                                      }
                                                }

                                                namespace {
                                                    //global code, for this to work the examples above would also
                                                    need to use bracketed syntax
                                                }

                                                $automobile = new autoAutomobile;
                                                $automobile->setType(‘none’);
                                                ?>

                                                Output:
                                                automobile
Late Static Bindings

•   Late Static Binding                    <?php
                                           class Automobile {
     – Stores the class name of the last         private function type() {
                                                   echo “Success!n”;
        “non-forwarded call”.                    }

                                                public function test() {
                                                  $this->type();
                                                  static::type();
                                                }
                                           }

                                           class Car extends Automobile {
                                                 // empty
                                           }

                                           Class Truck extends Automobile {
                                                 private function type() {
                                                   // empty
                                                 }
                                           }

                                           $car = new Car;
                                           $car->test();
                                           $truck = new Truck;
                                           $truck->test(); //fails because there is no test() in Truck
                                           ?>

                                           Output:
                                           Success!
                                           Success!
                                           Success!
                                           Fatal error: Call to private method Truck::type() from context
                                                 ‘Automobile’ in {file} on line n
Jump Labels (goto)

•   Jump Labels (goto)                         <?php
     – Used to jump to another section in      // some code here
       the program.
                                               goto a;
     – Target is specified by a label          echo ‘Foo’;
       followed by a colon.
     – Target must be within the same file     // more code here
       and context.
         • Cannot jump out of a function or    a:
           method, and cannot jump into one.
     – Cannot jump into any sort of loop       echo ‘Bar’;
       or switch structure, but may jump       ?>
       out.




                                               Output:
                                               Bar
Closures (Anonymous functions)

•   Closures (Anonymous functions)            <?php
                                              class Cart {
     – Allows the creation of functions             protected $products = array();
                                                    const PRICE_SHIRT = 20.00;
       which have no specific name.                 const PRICE_SCARF = 4.00;

     – Most useful as the value of callback        public function order() {
                                                     $this->products[‘shirt’] = 2;
       parameters.                                   $this->products[‘scarf’] = 3;
                                                   }
     – Can be used as the value of a
       variable.                                   public function getTotal($tax) {
                                                    $total = 0.00;
     – Can inherit variables from the                  $callback = function ($quantity, $product) use ($tax, &$total) {
       parent scope. (Not the same as                         $pricePerItem =
                                                              constant(__CLASS__ . “::PRICE_” . strtoupper($product));
       using global variables)
                                                              $total += ($pricePerItem * $quantity) * ($tax +1.0);
                                                       };
                                                       array_walk($this->products, $callback);

                                                       return round($total, 2);
                                                   }
                                              }
                                              ?>

                                              Output:
                                              55.64
Nested Exceptions

•   Nested Exceptions                           <?php
                                                class MyException extends Exception {}
     – Now your criteria within a “try” can
       also have another “try” nested           class Test {
       within, thus causing two levels of           public function testing() {
       failure to caught for further logging.         try {
                                                            try {
                                                                  throw new MyException(‘foo!’);
                                                            } catch (MyException $e) {
                                                                  throw $e;
                                                            }
                                                      } catch (Exception $e) {
                                                            var_dump($e->getMessage());
                                                      }
                                                    }
                                                }

                                                $foo = new Test;
                                                $foo->testing();
                                                ?>

                                                Output:
                                                String(4) “foo!”
New magic methods

•   __callStatic()
     – Used during overloading. Gives          No code to go with this…
       warning to enforce public visibility
       and non-static declaration.              yet. Experiment on your
         • Triggered when invoking
           inaccessible methods in a static     own.
           context.
• __invoke()
     – Called when a script tries to call an
       object as a function
Nowdoc

•   Nowdoc                                 <?php
     – nowdoc is similar to heredoc, but   echo <<<‘EOT’
       no parsing is done inside a nowdoc. My $foo->foo.
                                              name is “$name”. I am printing some
         • Ideal for code snippets or large      Now, I am printing some {$foo->bar[1]}.
           blocks of text without the need for   This should not print a capital ‘A’: /x41
           escaping.                             EOT;
         • Best used with static content.        ?>
     – Uses the same <<< sequence, but
       the following identifier is encosed
       in single quotes.


                                                 Output:
                                                 My name is “$name”. I am printing some
                                                    $foo->foo.
                                                 Now, I am printing some {$foo->bar[1]}.
                                                 This should not print a capital ‘A’: /x41
Heredoc changes

•   Heredoc                                <?php
                                           // Static variables
     – heredoc can now initialize static   function foo() {
       variables and class                     static $bar = <<<“LABEL”
       properties/constants.               Nothing in here…
     – Can now be declared (optional)      LABEL;
                                           }
       using double quotes,
       complementing the nowdoc syntax     // Class properties/constants
       which uses single quotes.           Class foo {
                                               const BAR = <<<FOOBAR
                                           Constant example
                                           FOOBAR;

                                               public $baz = <<<FOOBAR
                                           Property example
                                           FOOBAR
                                           }




                                           ?>
Constants addition

•   Constants addition                     <?php
     – A constant may now be declared      const TEST = ‘bar’;
       outside a class using the ‘const’
       keyword instead of ‘declare’.       Function foo() {
                                             echo ‘foo’;
                                           }

                                           foo();

                                           echo TEST;



                                           ?>
Ternary added shortcut

•                                              <?php
    Constants added shortcut
     – Can now use ternary for simpler         $test = true;
       returns of evaluation.                  // old way
     – Instead of defining the ‘middle’ part   $todo = ($test ? ‘Go’ : ‘Stop’);
       of the operation we simply get a ‘1’    echo $todo;
       if the first expression is true.
       Otherwise we receive what is in the
       third part, as we used to.              // added shortcut
                                               // if $test = true then we get a true flag, otherwise we get
                                                     the second expression as a result
                                               $tada = ($test ?: ‘whatever’);

                                               echo $tada;



                                               ?>

                                               Output (true):
                                               Go1

                                               Output (false):
                                               Stopwhatever
Questions and Resources


    South Florida PHP Users Group


           Adam Culp http://www.Geekyboy.com
           Email: adam@geekyboy.com


                     Resources:
     http://php.net/manual/en/migration53.new-
                     features.php

More Related Content

PPTX
Introduction to PHP OOP
PDF
Intermediate OOP in PHP
PPTX
Intro to OOP PHP and Github
PDF
OOP in PHP
PPT
Php Oop
PPTX
Object oreinted php | OOPs
PDF
A Gentle Introduction To Object Oriented Php
PPT
Oops in PHP
Introduction to PHP OOP
Intermediate OOP in PHP
Intro to OOP PHP and Github
OOP in PHP
Php Oop
Object oreinted php | OOPs
A Gentle Introduction To Object Oriented Php
Oops in PHP

What's hot (20)

PPTX
Oop in-php
PPT
Oops in PHP By Nyros Developer
PPT
Oops concepts in php
PPT
Class 7 - PHP Object Oriented Programming
PPTX
Object oriented programming in php 5
PPT
Introduction to OOP with PHP
ZIP
Object Oriented PHP5
PPTX
Php oop presentation
PPT
PHP- Introduction to Object Oriented PHP
PPT
Introduction to perl_ a scripting language
PPT
PHP - Introduction to Object Oriented Programming with PHP
PDF
Object Oriented Programming in PHP
PPTX
Oops in php
PDF
09 Object Oriented Programming in PHP #burningkeyboards
PDF
Object Oriented Programming with PHP 5 - More OOP
PPT
Class and Objects in PHP
PPTX
FFW Gabrovo PMG - PHP OOP Part 3
PDF
Introduction to php
PPT
Introduction to php php++
Oop in-php
Oops in PHP By Nyros Developer
Oops concepts in php
Class 7 - PHP Object Oriented Programming
Object oriented programming in php 5
Introduction to OOP with PHP
Object Oriented PHP5
Php oop presentation
PHP- Introduction to Object Oriented PHP
Introduction to perl_ a scripting language
PHP - Introduction to Object Oriented Programming with PHP
Object Oriented Programming in PHP
Oops in php
09 Object Oriented Programming in PHP #burningkeyboards
Object Oriented Programming with PHP 5 - More OOP
Class and Objects in PHP
FFW Gabrovo PMG - PHP OOP Part 3
Introduction to php
Introduction to php php++
Ad

Similar to Intro to OOP and new features in PHP 5.3 (20)

PDF
OOP in PHP
PDF
PHP 5.3 Overview
PDF
08 Advanced PHP #burningkeyboards
PDF
Giới thiệu PHP 7
PPTX
06-classes.ppt (copy).pptx
PPTX
PHP OOP Lecture - 02.pptx
PDF
Practical PHP 5.3
PDF
SPL: The Missing Link in Development
PPTX
PPT
Understanding PHP Functions: A Comprehensive Guide to Creating
PPTX
PHP: GraphQL consistency through code generation
PDF
Drupaljam xl 2019 presentation multilingualism makes better programmers
PPT
PHP-05-Objects.ppt
PPTX
Ch8(oop)
PDF
The Origin of Lithium
ODP
Symfony2, creare bundle e valore per il cliente
PDF
02 - Second meetup
PDF
Drupal 8 Services And Dependency Injection
ODP
Mastering Namespaces in PHP
PDF
Drupal 8's Multilingual APIs: Building for the Entire World
OOP in PHP
PHP 5.3 Overview
08 Advanced PHP #burningkeyboards
Giới thiệu PHP 7
06-classes.ppt (copy).pptx
PHP OOP Lecture - 02.pptx
Practical PHP 5.3
SPL: The Missing Link in Development
Understanding PHP Functions: A Comprehensive Guide to Creating
PHP: GraphQL consistency through code generation
Drupaljam xl 2019 presentation multilingualism makes better programmers
PHP-05-Objects.ppt
Ch8(oop)
The Origin of Lithium
Symfony2, creare bundle e valore per il cliente
02 - Second meetup
Drupal 8 Services And Dependency Injection
Mastering Namespaces in PHP
Drupal 8's Multilingual APIs: Building for the Entire World
Ad

More from Adam Culp (20)

PDF
Hypermedia
PDF
Putting legacy to REST with middleware
PDF
php-1701-a
PDF
Release your refactoring superpower
PDF
Managing Technical Debt
PDF
Developing PHP Applications Faster
PDF
Containing Quality
PDF
Debugging elephpants
PDF
Zend expressive workshop
PDF
Expressive Microservice Framework Blastoff
PDF
Foundations of Zend Framework
PDF
Accidental professional
PDF
Build great products
PDF
Does Your Code Measure Up?
PDF
Practical PHP Deployment with Jenkins
PDF
Virtualizing Development
PDF
Refactoring Legacy Code
PDF
Deprecated: Foundations of Zend Framework 2
PDF
Clean application development tutorial
PDF
Refactoring 101
Hypermedia
Putting legacy to REST with middleware
php-1701-a
Release your refactoring superpower
Managing Technical Debt
Developing PHP Applications Faster
Containing Quality
Debugging elephpants
Zend expressive workshop
Expressive Microservice Framework Blastoff
Foundations of Zend Framework
Accidental professional
Build great products
Does Your Code Measure Up?
Practical PHP Deployment with Jenkins
Virtualizing Development
Refactoring Legacy Code
Deprecated: Foundations of Zend Framework 2
Clean application development tutorial
Refactoring 101

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
MYSQL Presentation for SQL database connectivity
PPT
Teaching material agriculture food technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Building Integrated photovoltaic BIPV_UPV.pdf
NewMind AI Monthly Chronicles - July 2025
Network Security Unit 5.pdf for BCA BBA.
Modernizing your data center with Dell and AMD
NewMind AI Weekly Chronicles - August'25 Week I
MYSQL Presentation for SQL database connectivity
Teaching material agriculture food technology
Unlocking AI with Model Context Protocol (MCP)
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation_ Review paper, used for researhc scholars

Intro to OOP and new features in PHP 5.3

  • 1. OOP and PHP 5.3  South Florida PHP Users Group Adam Culp http://www.Geekyboy.com In this presentation we will talk about OOP, and discuss the object model progression in PHP from version 4 through the newest 5.3. I apologize in advance, but this will not be in great detail though I hope it is enough to help everyone in some way.
  • 2. Procedural? Really? • PHP was started as a procedural <?php function vehicle($wheels, $color, $hp) { tool to perform quick tasks. // build a car using info provided Large adoption brought more $car = array( users who wanted OOP, so here ‘wheels’ => $wheels, ‘color’ => $color, we are. ‘hp’ => $hp); – Everything went fairly sequential return $car; – Took a fair amount of labor within } code to change values afterward $car = vehicle(4, ‘red’, 240); – Procedural does make sense for // now continue with code that uses $car simple tasks, such as CRON jobs to If ($car[‘color’] == ‘red’) { perform quick tasks. echo ‘Your car is red’; } ?> Output: Your car is red Possible usage: Here is the car: <?php print_r($car); ?>
  • 3. OOP the basics • PHP 4 brought the introduction <?php Interface mobility { function setColor($color); of oop to PHP. } function setHorsepower($hp); – Application builds an object class Vehicle implements mobility { – Helps with DRY (Don’t repeat public $color; public $horsepower; yourself) function setColor($color) { – Simplifies structure, code still can } $this->color = $color; be sequential but is more ‘modular’ function setHorsepower($hp) { – Helps keep code more readable } $this->horsepower = $hp; } – Object can be manipulated easier prior to use of the object Class Car extends Vehicle { public $wheel; – We instantiate the class by creating function addWheel($n) { a “new” object, then manipulate the } $this->wheel += $n; object by calling the different } “methods” within the class. $myCar = new Car(); $myCar->setColor(‘red’); $myCar->setHorsepower(250); $myCar->addWheel(3); $myCar->addWheel(1); print_r($myCar); ?> Output: Car Object([wheel]=>4[color]=>red[horsepower]=>250)
  • 4. PHP 4 revisit • PHP 4 had a very basic OOP <?php class Reference { presence, and looked something var $reference; like this -> // acted as constructor – Variables need to be defined as function Reference() { ‘var’. $this->reference = ‘dictionary’; – Constructors carried the same name } as the Class. function getReference() { • Note: the constructor always runs return $this->reference; “automagically” when the class is } instantiated } – No visibility $rt = new Reference(); – No abstraction $reference = $rt->getReference; – Very simple and easy to use, but echo ‘reference:’ . $reference; lacked many features of other OOP ?> languages at the time. Output: reference: dictionary
  • 5. PHP 5.0 brought changes • With the rollout of PHP 5.0 there were many <?php class Reference { changes. const DEFAULT_LANG = ‘eng’; – Protected data with Visibility private $reference; • Public (default) – accessed and changed private $lang; globally public function __construct(Language $lang) { if($lang ) { • Protected – access and changed by $this->lang = $lang->esp; direct descendants } else { $this->lang = DEFAULT_LANG; • Private – access and changed within } class only $this->reference = ‘dictionary’; } – Type Hinting – notice how we specify that an public function getReference() { object of Language is passed to the return $this->reference; constructor. This means we must create an } object using the Language class first. private function setPrivateReference() { – Variables no longer need the ‘var’ keyword $this->reference = ‘my_dictionary’; } – Constructor now defined using __construct } call class Language { – CONSTANT values may now be assigned public $esp = ‘Spanish’; per-class, cannot be a variable or property or } mathematical operation or function call. $lang = new Language(); $rt = new Reference($lang); $reference = $rt->getReference; echo ‘reference:’ . $reference; ?> Output: Reference: Spanish
  • 6. PHP 5.0 Abstraction • Abstraction <?php abstract class Reference { – Abstraction, if a class contains any abstract public $reference; methods the class must also be abstract. abstract public function read(); – Abstracted methods must be defined by the public function __construct() { child class. $this->reference = ‘dictionary’; } – Visibility in the method of the child class must be the same, or less, restricted. final public function getReference() { return $this->reference; – “final” keyword prevents child classes from } overriding a method protected function setProtectedReference($myReference) { – “clone” creates a copy of an object rather $this->reference = $myReference; } than continuing to use the same object. } class Letter extends Reference { public function read($personal) { $myDictionary = $personal; parent::setProtectedReference($myDictionary); return $this->reference; } } $rt = new Letter(); $reference = $rt->read(‘my english dictionary’); $rt2 = clone $rt; echo ‘reference:’ . $reference; ?> Output: reference: my english dictionary
  • 7. PHP 5.0 Interfaces • Interface <?php interface rTemplate – Interface, specifies which methods a { public function getReference(); class must implement. public function setProtectedReference(); } – All methods in interface must be public. – Multiple interfaces can be implemented class Reference implements rTemplate { public $reference; by using comma separation public function __construct() { – Interface may contain a CONSTANT, $this->reference = ‘dictionary’; but may not be overridden by } implementing class public function getReference() { return $this->reference; } protected function setProtectedReference($myReference) { $this->reference = $myReference; } } $rt = new Letter(); $reference = $rt->getReference(); echo ‘reference:’ . $reference; ?> Output: reference: dictionary
  • 8. PHP 5.0 Exceptions • <?php Additional features with PHP 5. function inverse($x) { – Exceptions – throwing an exception if (!$x) { throw new Exception(‘Division by zero.’); to gracefully error out, while } else { continuing to execute the rest of the return 1/$x; script even after the error. } } • Notice how we still get ‘Hello World’ even after an exception try { caused by ‘Division by zero’. echo inverse(5) . “n”; echo inverse(0) . “n”; // trigger exception – Exceptions can then be caught for } catch (Exception $e) { logging, etc. echo ‘Caught exception: ‘, $e->getMessage(), “n”; } // continue execution echo ‘Hello World’; ?> Output: 0.2 Caught exception: Division by zero. Hello World
  • 9. Finally, PHP 5.3 features • Additional features with PHP 5.3 – Namespaces – Late Static Bindings – Jump labels (goto) – Closures – __callStatic() and __invoke() – Class Constants – Nowdoc syntax supported – Use Heredocs to initialize static variables and class properties – Heredocs with double quotes – Ternary operator shortcut – HTTP status 200 to 399 = success – Dynamic access to static methods – Exception nesting – mail() logging of sent email
  • 10. Namespaces • Namespaces <?php declare (encoding=‘UTF-8’); – Help create a new layer of code namespace automobile; encapsulation. class Automobile { • Keep properties from colliding function setType($type) { echo __NAMESPACE__ . “n”; between areas of your code } • Only classes, interfaces, functions } and constants are affected namespace automobilecar; – Anything that does not have a class Car { namespace is considered in the function toyota() { echo “test driven”; Global namespace (namespace = } “”) } – Namespace must be declared first $car = new Car $car->toyota(); (except ‘declare’ statement) // OR you can use the namespace – Can define multiple namespaces in $auto = new automobilecarCar; the same file. $auto->toyota(); ?> Output: test drive test drive
  • 11. Namespaces – cont. • Namespaces – cont. <?php namespace automobile; – You can define that something be class Automobile { used in the “Global” namespace by function setType($type) { echo __NAMESPACE__ . “n”; enclosing a non-labeled namespace } in {} brackets. (Note: if you have } multiple namespaces in the same namespace automobilecar; file they must all use this notation.) use automobile as auto; – Use namespaces from within other class Car { namespaces, along with aliasing function toyota() { echo “test driven”; } } namespace { //global code, for this to work the examples above would also need to use bracketed syntax } $automobile = new autoAutomobile; $automobile->setType(‘none’); ?> Output: automobile
  • 12. Late Static Bindings • Late Static Binding <?php class Automobile { – Stores the class name of the last private function type() { echo “Success!n”; “non-forwarded call”. } public function test() { $this->type(); static::type(); } } class Car extends Automobile { // empty } Class Truck extends Automobile { private function type() { // empty } } $car = new Car; $car->test(); $truck = new Truck; $truck->test(); //fails because there is no test() in Truck ?> Output: Success! Success! Success! Fatal error: Call to private method Truck::type() from context ‘Automobile’ in {file} on line n
  • 13. Jump Labels (goto) • Jump Labels (goto) <?php – Used to jump to another section in // some code here the program. goto a; – Target is specified by a label echo ‘Foo’; followed by a colon. – Target must be within the same file // more code here and context. • Cannot jump out of a function or a: method, and cannot jump into one. – Cannot jump into any sort of loop echo ‘Bar’; or switch structure, but may jump ?> out. Output: Bar
  • 14. Closures (Anonymous functions) • Closures (Anonymous functions) <?php class Cart { – Allows the creation of functions protected $products = array(); const PRICE_SHIRT = 20.00; which have no specific name. const PRICE_SCARF = 4.00; – Most useful as the value of callback public function order() { $this->products[‘shirt’] = 2; parameters. $this->products[‘scarf’] = 3; } – Can be used as the value of a variable. public function getTotal($tax) { $total = 0.00; – Can inherit variables from the $callback = function ($quantity, $product) use ($tax, &$total) { parent scope. (Not the same as $pricePerItem = constant(__CLASS__ . “::PRICE_” . strtoupper($product)); using global variables) $total += ($pricePerItem * $quantity) * ($tax +1.0); }; array_walk($this->products, $callback); return round($total, 2); } } ?> Output: 55.64
  • 15. Nested Exceptions • Nested Exceptions <?php class MyException extends Exception {} – Now your criteria within a “try” can also have another “try” nested class Test { within, thus causing two levels of public function testing() { failure to caught for further logging. try { try { throw new MyException(‘foo!’); } catch (MyException $e) { throw $e; } } catch (Exception $e) { var_dump($e->getMessage()); } } } $foo = new Test; $foo->testing(); ?> Output: String(4) “foo!”
  • 16. New magic methods • __callStatic() – Used during overloading. Gives No code to go with this… warning to enforce public visibility and non-static declaration. yet. Experiment on your • Triggered when invoking inaccessible methods in a static own. context. • __invoke() – Called when a script tries to call an object as a function
  • 17. Nowdoc • Nowdoc <?php – nowdoc is similar to heredoc, but echo <<<‘EOT’ no parsing is done inside a nowdoc. My $foo->foo. name is “$name”. I am printing some • Ideal for code snippets or large Now, I am printing some {$foo->bar[1]}. blocks of text without the need for This should not print a capital ‘A’: /x41 escaping. EOT; • Best used with static content. ?> – Uses the same <<< sequence, but the following identifier is encosed in single quotes. Output: My name is “$name”. I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should not print a capital ‘A’: /x41
  • 18. Heredoc changes • Heredoc <?php // Static variables – heredoc can now initialize static function foo() { variables and class static $bar = <<<“LABEL” properties/constants. Nothing in here… – Can now be declared (optional) LABEL; } using double quotes, complementing the nowdoc syntax // Class properties/constants which uses single quotes. Class foo { const BAR = <<<FOOBAR Constant example FOOBAR; public $baz = <<<FOOBAR Property example FOOBAR } ?>
  • 19. Constants addition • Constants addition <?php – A constant may now be declared const TEST = ‘bar’; outside a class using the ‘const’ keyword instead of ‘declare’. Function foo() { echo ‘foo’; } foo(); echo TEST; ?>
  • 20. Ternary added shortcut • <?php Constants added shortcut – Can now use ternary for simpler $test = true; returns of evaluation. // old way – Instead of defining the ‘middle’ part $todo = ($test ? ‘Go’ : ‘Stop’); of the operation we simply get a ‘1’ echo $todo; if the first expression is true. Otherwise we receive what is in the third part, as we used to. // added shortcut // if $test = true then we get a true flag, otherwise we get the second expression as a result $tada = ($test ?: ‘whatever’); echo $tada; ?> Output (true): Go1 Output (false): Stopwhatever
  • 21. Questions and Resources  South Florida PHP Users Group Adam Culp http://www.Geekyboy.com Email: adam@geekyboy.com Resources: http://php.net/manual/en/migration53.new- features.php