PHP Sucks?!
Simon Neidhold, Stefan Wiegreffe, ICANS GmbH
www.icans-gmbh.com




                                               1	
  
Über uns



Simon	
  Neidhold	
                   Stefan	
  Wiegreffe	
  
So&ware	
  Engineer	
                 Team	
  Lead	
  Web	
  Development	
  
simon.neidhold@icans-­‐gmbh.com	
     stefan.wiegreffe@icans-­‐gmbh.com	
  
@sneidhold	
                          @swiegreffe	
  




                                                                               2	
  
                                                                               2
PHP sucks?!




              3	
  
              3
Die Nadel im Heuhaufen




                         4	
  
                         4
to, _to_, 2




              5	
  
              5
Unterm Strich zählt die Semantik




                                   6	
  
                                   6
Next: Wordpress




                  7	
  
                  7
Standard-Template: index.php

<?php!
    /**!
       * The main template file.!
       *!
       * This is the most generic template file in a WordPress theme [...].!
       *!
       * @package WordPress!
       * @subpackage Twenty_Eleven!
       */!
    !
get_header(); ?>!
!
    <div id="primary">!
        <div id="content" role="main">!
!
        <?php if ( have_posts() ) : ?>!
!
            <?php twentyeleven_content_nav( 'nav-above' ); ?>!
!
            <?php /* Start the Loop */ ?>!
            <?php while ( have_posts() ) : the_post(); ?>!
!
                <?php get_template_part( 'content', get_post_format() ); ?>!
    !
            <?php endwhile; ?>!
!
            [...]!
                                                                               8	
  
                                                                               8
the_post()

    the_post()	
  ist	
  deklariert	
  in	
  Zeile	
  773	
  in	
  wp-­‐includes/query.php:	
  	
  

/**!
 * Iterate the post index in the loop.!
 *!
 * @see WP_Query::the_post()!
 * @since 1.5.0!
 * @uses $wp_query!
 */!
function the_post() {!
     global $wp_query;!
!
          $wp_query->the_post();!
}!




                                                                                                      9	
  
                                                                                                      9
WTF??

    WP_Query::the_post()	
  ist	
  deklariert	
  in	
  Zeile	
  2778	
  in	
  wp-­‐includes/query.php:	
  	
  

class WP_Query {!
!
         /**!
            * Sets up the current post.!
            *!
            * Retrieves the next post, sets up the post, sets the 'in the loop'!
            * property to true.!
            *!
            * @since 1.5.0!
            * @access public!
            * @uses $post!
            * @uses do_action_ref_array() Calls 'loop_start' if loop has just started!
            */!
         function the_post() {!
                global $post;!
                $this->in_the_loop = true;!
                !
                if ( $this->current_post == -1 ) // loop has just started!
                        do_action_ref_array('loop_start', array(&$this));!
                  !
                    $post = $this->next_post();!
                setup_postdata($post);!
         }!
!
}	
                                                                                                              10	
  
                                                                                                                 10
Next: Debugging!



<?php!
    require "../../global_setting.php";!
    include "../../custom/functions.php" ;!
      !
        $someId = 1337;!
        $sql = "INSERT INTO some_table (`name`, `date`, `count`) ";!
        $sql .= "VALUES ('{$someId}', " . time() - mktime(0,0,0) . " , 6)";!
        !
    // echo '$sql: '; !
    // var_dump($sql);!
    // die();!
    !




Die	
  Debug-­‐Ausgabe	
  wäre	
  übrigens	
  $sql:	
  string(16)	
  "-­‐1322434800	
  ,	
  6)"	
  




                                                                                                      11	
  
                                                                                                      11
Schwache, dynamische Typisierung



echo   (true == "false");!
  =>   1!
echo   (true === "false");!
  =>   !
echo   (true == "");!
  =>     !
!
echo   (10 + 012);!
  =>   20!
echo   (10 + 09);!
  =>   10!
!
echo   010 + 5;!
  =>   13!
echo   "010" + 5;!
  =>   15!
!




                                   12	
  
                                   12
World Wide Web




                 13	
  
                 13
Twitter JSON API
{!
     "completed_in":0.209,!
     "max_id":141283786544513025,!
     "max_id_str":"141283786544513025",!
     "next_page":"?page=2&max_id=141283786544513025&q=php%20sucks&rpp=1",!
     "page":1,!
     "query":"php+sucks",!
     "refresh_url":"?since_id=141283786544513025&q=php%20sucks",!
     "results":[{"created_at":"Mon,!
      28 Nov 2011 22:34:14 +0000",!
     "from_user":"infomancien",!
     "from_user_id":79284740,!
     "from_user_id_str":"79284740",!
     "from_user_name":"Infomancien",!
     "geo":null,!
     "id":141283786544513025,!
     "id_str":"141283786544513025",!
     "iso_language_code":"en",!
     "metadata":{"result_type":"recent"},!
     "profile_image_url":"http://a3.twimg.com/[...]/alicelascombe_normal.png",!
     "source":"&lt;a href=&quot;http://www.hootsuite.com[...]",!
     "text":"(@rww) Entrepreneurship Sucks - The sad state of affairs is that working !
             for yourself is really just a worse version... http://t.co/BEctTyFw",!
     "to_user":null,!
     "to_user_id":null,!
     "to_user_id_str":null,!
     "to_user_name":null}],!
     "results_per_page":1,!
     "since_id":0,!
     "since_id_str":"0"!
}!

                                                                                          14	
  
                                                                                          14
(Rapid) Prototyping FTW!




                           15	
  
                           15
Vom Prototyp zum Produkt




                           ???




                                 16	
  
                                 16
Das finale Produkt mit echter Technologien umsetzen




§  Enterprise-Grade Application Server

§  Spring

§  Java Server Faces

§  Hibernate

§  Junit




                                                      17	
  
                                                      17
PHP ist (uns) bereits bekannt.




                                 18	
  
Application Server


§  Ein beliebiger Webserver

§  Schmaler Footprint

 •  PokerStrategy.com: maximal 8MB je Request
 •  JBoss + Liferay: 1,2 GB für Bootstrapping




                                                19	
  
                                                19
Application Server / Skalierung über FPM




                                           20	
  
                                           20
Spring / Java Server Faces




•  Weit verbreitet            •  100% OOP
•  Beeindruckender Umfang     •  Lose gekoppelt
 - PDF- Handling              •  Annotations
 - Social Media Connectoren   •  Dependency Injection




                                                        21	
  
                                                        21
Hibernate / JUnit




 DBAL	
  /	
  ORM	
  für	
     Dynamic	
  Mocks	
  
 -­‐  MySQL	
  
 -­‐  SQLite	
  
 -­‐  PostgreSQL	
  
 -­‐  MSSQL	
  
 -­‐  Oracle	
  
 	
  
 ODM	
  für	
  
 -­‐  CouchDB	
  
 -­‐  MongoDB	
  
 -­‐  PHPCR	
  


                                                      22	
  
                                                      22
Nicht nur das PHP-Ökosystem rockt.




                                     23	
  
PHP arbeitet wie das WWW




                  §  ... ist Request-Basiert

                  §  ... hat keine strikte Typisierung




                                                          24	
  
                                                          24
Batteries included



§  XML, Image/Audio-Manipulation, Zip-Files, OAuth, Cryptography

§  mächtige String- und Array-Funktionen

§  Reflection

§  PECL/Pear




   USE	
                    IMPORT	
                     DLLs	
  

                                                                    25	
  
                                                                    25
Auch das ist PHP


§  Autoloading

§  Namespacing

§  Type Hinting

§  Anonyme Funktionen

§  Abstrakte Klassen

§  Interfaces

§  private, protected, public, final

§  Exceptions




                                        26	
  
                                        26
Sprache oder Entwickler – Wer suckt denn nun?




               Mit	
  PHP	
  kann	
  man	
  viel	
  erreichen	
  


               Es	
  ist	
  sehr	
  wichWg,	
  gute	
  Entwickler	
  zu	
  haben	
  




                                                                                       27	
  
                                                                                       27
Erfolgreich mit PHP




                      28	
  
                      28
PokerStrategy.com in Zahlen


§  70 Mio Seitenabrufe / Monat
                                                                7%	
  
                                                  7%	
   8%	
  
§  6 Mio Reg. User

§  500.000 LOC                                                               14%	
  
                                                           3%	
   4%	
  
                                                             3%	
  
§  >50 TB Traffic / Monat

§  19 Sprachen                                                              6%	
  

                                         4%	
     Rest	
  of	
  the	
  world:	
  
§  Insgesamt ~50 Server
 •  2 Load Balancer
 •  10 App-Server
                                     3%	
  
                                                      41%	
  
 •  15 DB-Server
 •  Video Delivery, Community, ...




                                                                                        29	
  
                                                                                        29
Case Study: ID-Check


                                        CASE STUDY
§  Problem: Fraud

§  Validierung des Nutzers notwendig

§  Upload von Dokumenten




                                                30	
  
                                                30
ID-Check: Screenshot


                       CASE STUDY




                               31	
  
                               31
ID-Check: Requirements


§  Privacy                                         CASE STUDY
 •  Nur Thumbnail nach extern ausliefern
 •  nur an den Uploader

§  Performance
 •  Uploads (bis 20MB JPEG) in 2 Größen umrechnen
 •  Erwartete Last:
   -  Max. 10.000 Bilder / Tag
   -  Max. 4.000 Bilder / Stunde

§  Security
 •  Lastkontrolle / Missbrauch verhindern
 •  Viren-Check

§  Funktional
 •  Thumbnail für User
 •  Preview in mittlerer Größe

                                                            32	
  
                                                            32
ID-Check: Basic Setup


                                                                            CASE STUDY




                                            ID-Check Server




                                                 API



                                     HTTP                     HTTP



                        AJAX Proxy


                                                                     Backoffice
                         Frontend




                                                                                    33	
  
                                                                                    33
ID-Check: XSRF-Protection


                                                                                CASE STUDY




                                                ID-Check Server


                            Validate
               Datenbank     Token



                                                     API



                Create                   HTTP                     HTTP
                Token


                            AJAX Proxy


                                                                         Backoffice
                              Frontend




                                                                                        34	
  
                                                                                        34
Uploads von nicht angemeldeten Usern verhindern


                                                                                       CASE STUDY

                           Check
 SSO Service               Login
                           State

                                                       ID-Check Server


                                   Validate
               Datenbank            Token



                                                            API
      Check
      Login
      State
                Create                          HTTP                     HTTP
                Token


                                   AJAX Proxy


                                                                                Backoffice
                                     Frontend




                                                                                               35	
  
                                                                                               35
ID-Check: Performance / Skalierung


                                                      CASE STUDY




§  4 Worker / 4 Kerne

§  Synchroner Prozess für Anti-Virus und Thumbnail

§  Asynchron für mittlere Größe




                                                              36	
  
                                                              36
ID-Check: Fazit


                                                                CASE STUDY




                  §  Entwicklungszeit: 8 Wochen

                  §  Server virtualisiert ohne Last-Probleme

                                                                        37	
  
                                                                        37
„PHP-Entwickler sind aber @!#*$“




                                   38	
  
Auch / Gerade bei PHP: Maximale Qualitätsstandards


§  Nur exzellente Entwickler einstellen

§  Continuous Improvement
   •  Konferenzen, Zertifizierungen, Schulungen, Pair Programming, ...

§  Continuous Integration (Jenkins)
   •  Code Sniffer, Mess Detection
   •  Coding Standards, Unit-Tests

§  Regelmäßige Code – Reviews

§  ...




                                                                         39	
  
                                                                         39
Roundup


§  Man kann in jeder Sprache Schrott machen

§  Der Entwickler macht den Unterschied

§  Die gewählte Architektur ist wichtiger als die gewählte Sprache



§  Für ICANS ist PHP auch langfristig die richtige Entscheidung




                                                                      40	
  
                                                                      40
www.icans-­‐gmbh.com	
  
                           41	
  
ICANS GmbH
Valentinskamp 18
20354 Hamburg
Germany


Phone:   +49 40 22 63 82 9-0
Fax:     +49 40 38 67 15 92


Web: www.icans-gmbh.com
	
  



                               42	
  

PHP Sucks?!

  • 1.
    PHP Sucks?! Simon Neidhold,Stefan Wiegreffe, ICANS GmbH www.icans-gmbh.com 1  
  • 2.
    Über uns Simon  Neidhold   Stefan  Wiegreffe   So&ware  Engineer   Team  Lead  Web  Development   simon.neidhold@icans-­‐gmbh.com   stefan.wiegreffe@icans-­‐gmbh.com   @sneidhold   @swiegreffe   2   2
  • 3.
  • 4.
    Die Nadel imHeuhaufen 4   4
  • 5.
  • 6.
    Unterm Strich zähltdie Semantik 6   6
  • 7.
  • 8.
    Standard-Template: index.php <?php! /**! * The main template file.! *! * This is the most generic template file in a WordPress theme [...].! *! * @package WordPress! * @subpackage Twenty_Eleven! */! ! get_header(); ?>! ! <div id="primary">! <div id="content" role="main">! ! <?php if ( have_posts() ) : ?>! ! <?php twentyeleven_content_nav( 'nav-above' ); ?>! ! <?php /* Start the Loop */ ?>! <?php while ( have_posts() ) : the_post(); ?>! ! <?php get_template_part( 'content', get_post_format() ); ?>! ! <?php endwhile; ?>! ! [...]! 8   8
  • 9.
    the_post() the_post()  ist  deklariert  in  Zeile  773  in  wp-­‐includes/query.php:     /**! * Iterate the post index in the loop.! *! * @see WP_Query::the_post()! * @since 1.5.0! * @uses $wp_query! */! function the_post() {! global $wp_query;! ! $wp_query->the_post();! }! 9   9
  • 10.
    WTF?? WP_Query::the_post()  ist  deklariert  in  Zeile  2778  in  wp-­‐includes/query.php:     class WP_Query {! ! /**! * Sets up the current post.! *! * Retrieves the next post, sets up the post, sets the 'in the loop'! * property to true.! *! * @since 1.5.0! * @access public! * @uses $post! * @uses do_action_ref_array() Calls 'loop_start' if loop has just started! */! function the_post() {! global $post;! $this->in_the_loop = true;! ! if ( $this->current_post == -1 ) // loop has just started! do_action_ref_array('loop_start', array(&$this));! ! $post = $this->next_post();! setup_postdata($post);! }! ! }   10   10
  • 11.
    Next: Debugging! <?php! require "../../global_setting.php";! include "../../custom/functions.php" ;! ! $someId = 1337;! $sql = "INSERT INTO some_table (`name`, `date`, `count`) ";! $sql .= "VALUES ('{$someId}', " . time() - mktime(0,0,0) . " , 6)";! ! // echo '$sql: '; ! // var_dump($sql);! // die();! ! Die  Debug-­‐Ausgabe  wäre  übrigens  $sql:  string(16)  "-­‐1322434800  ,  6)"   11   11
  • 12.
    Schwache, dynamische Typisierung echo (true == "false");! => 1! echo (true === "false");! => ! echo (true == "");! => ! ! echo (10 + 012);! => 20! echo (10 + 09);! => 10! ! echo 010 + 5;! => 13! echo "010" + 5;! => 15! ! 12   12
  • 13.
  • 14.
    Twitter JSON API {! "completed_in":0.209,! "max_id":141283786544513025,! "max_id_str":"141283786544513025",! "next_page":"?page=2&max_id=141283786544513025&q=php%20sucks&rpp=1",! "page":1,! "query":"php+sucks",! "refresh_url":"?since_id=141283786544513025&q=php%20sucks",! "results":[{"created_at":"Mon,! 28 Nov 2011 22:34:14 +0000",! "from_user":"infomancien",! "from_user_id":79284740,! "from_user_id_str":"79284740",! "from_user_name":"Infomancien",! "geo":null,! "id":141283786544513025,! "id_str":"141283786544513025",! "iso_language_code":"en",! "metadata":{"result_type":"recent"},! "profile_image_url":"http://a3.twimg.com/[...]/alicelascombe_normal.png",! "source":"&lt;a href=&quot;http://www.hootsuite.com[...]",! "text":"(@rww) Entrepreneurship Sucks - The sad state of affairs is that working ! for yourself is really just a worse version... http://t.co/BEctTyFw",! "to_user":null,! "to_user_id":null,! "to_user_id_str":null,! "to_user_name":null}],! "results_per_page":1,! "since_id":0,! "since_id_str":"0"! }! 14   14
  • 15.
  • 16.
    Vom Prototyp zumProdukt ??? 16   16
  • 17.
    Das finale Produktmit echter Technologien umsetzen §  Enterprise-Grade Application Server §  Spring §  Java Server Faces §  Hibernate §  Junit 17   17
  • 18.
    PHP ist (uns)bereits bekannt. 18  
  • 19.
    Application Server §  Einbeliebiger Webserver §  Schmaler Footprint •  PokerStrategy.com: maximal 8MB je Request •  JBoss + Liferay: 1,2 GB für Bootstrapping 19   19
  • 20.
    Application Server /Skalierung über FPM 20   20
  • 21.
    Spring / JavaServer Faces •  Weit verbreitet •  100% OOP •  Beeindruckender Umfang •  Lose gekoppelt - PDF- Handling •  Annotations - Social Media Connectoren •  Dependency Injection 21   21
  • 22.
    Hibernate / JUnit DBAL  /  ORM  für   Dynamic  Mocks   -­‐  MySQL   -­‐  SQLite   -­‐  PostgreSQL   -­‐  MSSQL   -­‐  Oracle     ODM  für   -­‐  CouchDB   -­‐  MongoDB   -­‐  PHPCR   22   22
  • 23.
    Nicht nur dasPHP-Ökosystem rockt. 23  
  • 24.
    PHP arbeitet wiedas WWW §  ... ist Request-Basiert §  ... hat keine strikte Typisierung 24   24
  • 25.
    Batteries included §  XML,Image/Audio-Manipulation, Zip-Files, OAuth, Cryptography §  mächtige String- und Array-Funktionen §  Reflection §  PECL/Pear USE   IMPORT   DLLs   25   25
  • 26.
    Auch das istPHP §  Autoloading §  Namespacing §  Type Hinting §  Anonyme Funktionen §  Abstrakte Klassen §  Interfaces §  private, protected, public, final §  Exceptions 26   26
  • 27.
    Sprache oder Entwickler– Wer suckt denn nun? Mit  PHP  kann  man  viel  erreichen   Es  ist  sehr  wichWg,  gute  Entwickler  zu  haben   27   27
  • 28.
  • 29.
    PokerStrategy.com in Zahlen § 70 Mio Seitenabrufe / Monat 7%   7%   8%   §  6 Mio Reg. User §  500.000 LOC 14%   3%   4%   3%   §  >50 TB Traffic / Monat §  19 Sprachen 6%   4%   Rest  of  the  world:   §  Insgesamt ~50 Server •  2 Load Balancer •  10 App-Server 3%   41%   •  15 DB-Server •  Video Delivery, Community, ... 29   29
  • 30.
    Case Study: ID-Check CASE STUDY §  Problem: Fraud §  Validierung des Nutzers notwendig §  Upload von Dokumenten 30   30
  • 31.
    ID-Check: Screenshot CASE STUDY 31   31
  • 32.
    ID-Check: Requirements §  Privacy CASE STUDY •  Nur Thumbnail nach extern ausliefern •  nur an den Uploader §  Performance •  Uploads (bis 20MB JPEG) in 2 Größen umrechnen •  Erwartete Last: -  Max. 10.000 Bilder / Tag -  Max. 4.000 Bilder / Stunde §  Security •  Lastkontrolle / Missbrauch verhindern •  Viren-Check §  Funktional •  Thumbnail für User •  Preview in mittlerer Größe 32   32
  • 33.
    ID-Check: Basic Setup CASE STUDY ID-Check Server API HTTP HTTP AJAX Proxy Backoffice Frontend 33   33
  • 34.
    ID-Check: XSRF-Protection CASE STUDY ID-Check Server Validate Datenbank Token API Create HTTP HTTP Token AJAX Proxy Backoffice Frontend 34   34
  • 35.
    Uploads von nichtangemeldeten Usern verhindern CASE STUDY Check SSO Service Login State ID-Check Server Validate Datenbank Token API Check Login State Create HTTP HTTP Token AJAX Proxy Backoffice Frontend 35   35
  • 36.
    ID-Check: Performance /Skalierung CASE STUDY §  4 Worker / 4 Kerne §  Synchroner Prozess für Anti-Virus und Thumbnail §  Asynchron für mittlere Größe 36   36
  • 37.
    ID-Check: Fazit CASE STUDY §  Entwicklungszeit: 8 Wochen §  Server virtualisiert ohne Last-Probleme 37   37
  • 38.
  • 39.
    Auch / Geradebei PHP: Maximale Qualitätsstandards §  Nur exzellente Entwickler einstellen §  Continuous Improvement •  Konferenzen, Zertifizierungen, Schulungen, Pair Programming, ... §  Continuous Integration (Jenkins) •  Code Sniffer, Mess Detection •  Coding Standards, Unit-Tests §  Regelmäßige Code – Reviews §  ... 39   39
  • 40.
    Roundup §  Man kannin jeder Sprache Schrott machen §  Der Entwickler macht den Unterschied §  Die gewählte Architektur ist wichtiger als die gewählte Sprache §  Für ICANS ist PHP auch langfristig die richtige Entscheidung 40   40
  • 41.
  • 42.
    ICANS GmbH Valentinskamp 18 20354Hamburg Germany Phone: +49 40 22 63 82 9-0 Fax: +49 40 38 67 15 92 Web: www.icans-gmbh.com   42