PHP on Java 
      
     
      
       Robin Fernandes   (ibm) 
       [email_address] 
       @rewbs

      
       PHP ? 
      
     
      
       “ PHP isn't so much a language as a   virtual explosion  at the  keyword and function factory .” 
       - Jeff Atwood 
       from “PHP sucks, but it doesn't matter”

      
       PHP ? 
      
     
      
       in_array( $needle ,  $haystack ); 
       strpos( $haystack ,  $needle ); 
      
     
      
       ... 
       IteratorIterator 
       RecursiveIterator 
       RecursiveIteratorIterator 
       ... 
      
     
      
       strip _ tags() 
       strip s lashes() 
       SDO _ Model _ Property::get C ontaining T ype() 
      
     
      
       ReflectionClass::isItera tea ble()

      
       PHP ! 
      
     
      
       
       
      
     
      
       
      
     
      
       
      
     
      
       
      
     
      
       
      
     
      
       
      
     
      
       
      
     
      
       
      
     
      
       tiobe.com

      
       PHP ! 
      
     
      
       
       
      
     
      
       
      
     
      
       Photo by Michelangelo van Dam ( DragonBe )

      
       PHP !

      
       Why  integrate PHP and Java?  
      
     
      
       
       
      
     
      
       
        
         PHP Developers to grow from 3 to  5.5 million by 2013 
        
        
         PHP Developers in Corporate IT to grow from 13% to  60% by 2013 
        
       
       
       “ Pay special attention to opportunities to use  PHP in combination with Java  development efforts” 
      
     
      
       
        
         - Gartner

      
       Why  run PHP  on the  JVM ?  
      
     
      
       
       
      
     
      
       
        
         PHP is a  powerful and pragmatic  language with a strong user-base 
         
        
        
         The Java Virtual Machines is a  cutting edge  language runtime 
        
       
       
       PHP + JVM =  ♥

      
       How  PHP  runs on the  JVM 
      
     
      <?php 
      echo  'hello' ; 
      
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      System. out .println( &quot;hello&quot; );

      
       How  PHP  runs on the  JVM 
      
     
      
       
        
         P8: a PHP 5.2 runtime implemented in Java 
        
        
         Runs on any Java 5+ JDK 
        
       
      
     
      
       
        
         Extensibility 
         
          
           XAPI-C for existing C extensions  
          
          
           XAPI-J for new Java extensions 
          
         
         (extension language choice is opaque to scripts) 
        
        
         
         Debug via DBGp  using Eclipse PDT 
        
       
      
     
      
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      P8 Runtime 
      
     
      SAPI-J 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      Parser 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      Interpreter 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      P8 Engine 
      
     
      Java  
      Extensions 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      C 
      Extensions 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      Stack 
      
     
      Resources 
      
     
      Classes 
      
     
      Objects 
      
     
      Variables 
      
     
      AST 
      
     
      XAPI-J 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      XAPI-C 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      Native  
      code 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      Compiler 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      .class 
      
       
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      opcodes 
      
     
      Cache 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      Persistent 
      class cache 
      
     
      
       
       
        
        
        
        
        
        
        
        
        
        
        
        
       
      
      
       Debug API  (DBGp) 
       
      
     
      
       
       
        
        
        
        
        
        
        
        
        
        
        
        
       
      
      
       Runtime

      
       Some  Code ! 
      
     
      <?php 
      java_import( &quot;java.util.HashMap&quot; ); 
      $map  =  new  HashMap; 
      
      $map ->put( &quot;stuff&quot; ,  array ( 1 , 2 , 3 , 4 , 5 )); 
      var_dump( $map ->get( &quot;stuff&quot; )); 
      // prints: array(5) { ...

      
       Some more  Code ! 
      
     
      <?php 
      java_import( &quot;java.io.File&quot; ); 
      
      class  SubFile  extends  File { 
      function  isThisCool() { 
      return TRUE ;  // Way cool 
      } 
      } 
      
      $file  =  new  SubFile( &quot;/&quot; ); 
      var_dump( $file ->isDirectory()); 
      var_dump( $file ->isThisCool());

      
       Some more  Code ! 
      
     
      <?php 
      java_import( &quot;java.util.ArrayList&quot; ); 
      
      $list  =  new  ArrayList(); 
      $list ->add( &quot;Hello World!&quot; ); 
      $list ->add( FALSE ); 
      $list ->add( 1234567890 ); 
      
      foreach  ( $list  as  $key  =>  $value ) { 
      echo  &quot; $key   $value \n&quot; ; 
      }

      
       PHP on Java:  Challenges  
      
     
      
       
        
         PHP has no language specification! 
         
          
           ->  Write tests & contribute them back to php.net 
          
         
        
        
         Keeping up-to-date with php.net 
         
          
           ->  As above! 
          
         
        
        
         Strings: byte array vs java.lang.String 
         
          
           ->  Use byte arrays, convert sparingly. 
          
         
        
        
         Class format limitations 
         
          
           ->  Use both compilation and interpretation.

      
       PHP on Java:  Challenges  
      
     
      
       
        
         Extension functions & symbol tables 
        
       
      
     
      
       ✔ 
      
     
      
       ✔ 
      
     
      
       ✘ 
      
     
      
       ✘ 
      
     
      function  f() { 
      $a  =  1 ; 
      return  $a ; 
      } 
      echo  f();  //1 
      
     
      function  f( $name ) { 
      $a  =  1 ; 
      $ $name  =  2 ; 
      return  $a ; 
      } 
      echo  f( 'a' );  //2 
      
     
      function  f() { 
      $a  =  1 ; 
      extract( array ( 'a' => 3 )); 
      return  $a ; 
      } 
      echo  f();  //3 
      
     
      function  f() { 
      $a  =  1 ; 
      print_r( array ( 'a' => 3 )); 
      return  $a ; 
      } 
      echo  f();  //3

      
       PHP apps  +  Java libraries 
      
     
      
       
        
         For example... 
         
         
          
           SugarCRM + Apache POI 
           
          
          
           Drupal + Eclipse BIRT 
           
          
          
           PhpBB + Apache Lucene

      
       Java apps  +  PHP scripting 
      
     
      
       
       
      
     
      
       
        
         For example...

      
       PHP in  WebSphere sMash 
      
     
      
       
       
      
     
      
       
        
         Build Web apps  quickly and easily 
         in  Java ,  Groovy  and  PHP . 
        
       
      
     
      
       
        
         Lightweight web app runtime 
        
        
         Browser-based development tooling 
        
        
         Try it now:  http://bit.ly/ibmCloud 
        
       
      
     
      
       
      
     
      
       
      
     
      
       
      
     
      JVM 
      
      
      
      
      
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      Web Server 
      
     
      Application Code 
      
     
      Language Runtimes

      
       One more bit of   Code ! 
      
     
      
       
      
     
      
       
      
     
      class Dynamic { 
      def  storage = [:] 
      def  invokeMethod(String name, args) { 
      println   &quot;Hello!&quot; 
      } 
      def  getProperty(String name) {  
      storage[name]  
      } 
      def  setProperty(String name, value) {  
      storage[name] = value  
      } 
      } 
      
     
      <?php 
      groovy_import( &quot;dynamic.groovy&quot; ); 
      $foo  =  new  Dynamic(); 
      $foo ->bar(); 
      $foo ->guff =  &quot;Hello World!&quot; ; 
      echo  $foo ->guff;

      
       PHP in  Message Broker 
      
     
      
       
       
      
     
      
       
       
      
     
      
       
        
         Message Broker “ connects anything to anything ” 
         
        
        
         Routes  and  transforms  data along the way 
         
        
        
         Use graphical mapping, XSLT, ESQL, Java and  now PHP ! 
         
        
        
         With PHP, use a SimpleXML-inspired syntax to easily manipulate XML message formats.

      
       PHP in  CICS 
      
     
      
       
       
      
     
      
       
        
         Build RESTful services 
         based on  CICS assets  using  PHP 
        
       
      
     
      
       
      
     
      
       
      
     
      Transaction Server 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      CICS assets: 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      RESTful API 
      to CICS assets  
      
     
      SupportPac CA1S 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      ... 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      DB2 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      COBOL 
      
       
       
       
       
       
       
       
       
       
       
       
       
      
     
      Java

      
       Learn  more & Try  it out: 
      
     
      
       
       
      
     
      
       Thanks! 
       [email_address]   /  @rewbs 
      
     
      
       http://projectzero.org/php 
       
       
       http://bit.ly/ibmCloud

Php On Java (London Java Community Unconference)

  • 1.
    PHP on Java Robin Fernandes (ibm) [email_address] @rewbs
  • 2.
    PHP ? “ PHP isn't so much a language as a virtual explosion at the keyword and function factory .” - Jeff Atwood from “PHP sucks, but it doesn't matter”
  • 3.
    PHP ? in_array( $needle , $haystack ); strpos( $haystack , $needle ); ... IteratorIterator RecursiveIterator RecursiveIteratorIterator ... strip _ tags() strip s lashes() SDO _ Model _ Property::get C ontaining T ype() ReflectionClass::isItera tea ble()
  • 4.
    PHP ! tiobe.com
  • 5.
    PHP ! Photo by Michelangelo van Dam ( DragonBe )
  • 6.
    PHP !
  • 7.
    Why integrate PHP and Java? PHP Developers to grow from 3 to 5.5 million by 2013 PHP Developers in Corporate IT to grow from 13% to 60% by 2013 “ Pay special attention to opportunities to use PHP in combination with Java development efforts” - Gartner
  • 8.
    Why run PHP on the JVM ? PHP is a powerful and pragmatic language with a strong user-base The Java Virtual Machines is a cutting edge language runtime PHP + JVM = ♥
  • 9.
    How PHP runs on the JVM <?php echo 'hello' ; System. out .println( &quot;hello&quot; );
  • 10.
    How PHP runs on the JVM P8: a PHP 5.2 runtime implemented in Java Runs on any Java 5+ JDK Extensibility XAPI-C for existing C extensions XAPI-J for new Java extensions (extension language choice is opaque to scripts) Debug via DBGp using Eclipse PDT P8 Runtime SAPI-J Parser Interpreter P8 Engine Java Extensions C Extensions Stack Resources Classes Objects Variables AST XAPI-J XAPI-C Native code Compiler .class opcodes Cache Persistent class cache Debug API (DBGp) Runtime
  • 11.
    Some Code ! <?php java_import( &quot;java.util.HashMap&quot; ); $map = new HashMap; $map ->put( &quot;stuff&quot; , array ( 1 , 2 , 3 , 4 , 5 )); var_dump( $map ->get( &quot;stuff&quot; )); // prints: array(5) { ...
  • 12.
    Some more Code ! <?php java_import( &quot;java.io.File&quot; ); class SubFile extends File { function isThisCool() { return TRUE ; // Way cool } } $file = new SubFile( &quot;/&quot; ); var_dump( $file ->isDirectory()); var_dump( $file ->isThisCool());
  • 13.
    Some more Code ! <?php java_import( &quot;java.util.ArrayList&quot; ); $list = new ArrayList(); $list ->add( &quot;Hello World!&quot; ); $list ->add( FALSE ); $list ->add( 1234567890 ); foreach ( $list as $key => $value ) { echo &quot; $key $value \n&quot; ; }
  • 14.
    PHP on Java: Challenges PHP has no language specification! -> Write tests & contribute them back to php.net Keeping up-to-date with php.net -> As above! Strings: byte array vs java.lang.String -> Use byte arrays, convert sparingly. Class format limitations -> Use both compilation and interpretation.
  • 15.
    PHP on Java: Challenges Extension functions & symbol tables ✔ ✔ ✘ ✘ function f() { $a = 1 ; return $a ; } echo f(); //1 function f( $name ) { $a = 1 ; $ $name = 2 ; return $a ; } echo f( 'a' ); //2 function f() { $a = 1 ; extract( array ( 'a' => 3 )); return $a ; } echo f(); //3 function f() { $a = 1 ; print_r( array ( 'a' => 3 )); return $a ; } echo f(); //3
  • 16.
    PHP apps + Java libraries For example... SugarCRM + Apache POI Drupal + Eclipse BIRT PhpBB + Apache Lucene
  • 17.
    Java apps + PHP scripting For example...
  • 18.
    PHP in WebSphere sMash Build Web apps quickly and easily in Java , Groovy and PHP . Lightweight web app runtime Browser-based development tooling Try it now: http://bit.ly/ibmCloud JVM Web Server Application Code Language Runtimes
  • 19.
    One more bit of Code ! class Dynamic { def storage = [:] def invokeMethod(String name, args) { println &quot;Hello!&quot; } def getProperty(String name) { storage[name] } def setProperty(String name, value) { storage[name] = value } } <?php groovy_import( &quot;dynamic.groovy&quot; ); $foo = new Dynamic(); $foo ->bar(); $foo ->guff = &quot;Hello World!&quot; ; echo $foo ->guff;
  • 20.
    PHP in Message Broker Message Broker “ connects anything to anything ” Routes and transforms data along the way Use graphical mapping, XSLT, ESQL, Java and now PHP ! With PHP, use a SimpleXML-inspired syntax to easily manipulate XML message formats.
  • 21.
    PHP in CICS Build RESTful services based on CICS assets using PHP Transaction Server CICS assets: RESTful API to CICS assets SupportPac CA1S ... DB2 COBOL Java
  • 22.
    Learn more & Try it out: Thanks! [email_address] / @rewbs http://projectzero.org/php http://bit.ly/ibmCloud