PHP5.4@tanakahisateru
ABOUT ME•              (               )• @tanakahisateru• https://github.com/tanakahisateru• Firebug, FireCookie, jEdit• ...
•   PHP5.4•   Array Short Syntax•   Built-in Server•   Trait•   Closure•
:
PHP5.4Graham     ( @predominant )                                  :)     http://tipshare.info/view/   4ec326d04b2122ce490...
PHP5.4
PHP 5.4 RC1 (2011/11/11)• http://www.php.net/archive/2011.php#id2011-11-11-1
PHP          PHP 5.4    http://www.1x1.jp/blog/            2011/06/try_new_php_without_update     _current_version.html   ...
•   Windows    •   http://windows.php.net/qa/•   Mac    •   XCode + MacPorts or Homebrew•   Linux    •
% curl -o php-5.4.0RC1.tar.gz http://downloads.php.net/stas/php-5.4.0RC1.tar.gz% tar xzf php-5.4.0RC1.tar.gz% cd php-5.4.0...
(make install)sapi/cli/php      configure         PHP
% sapi/cli/php -vPHP 5.4.0RC1 (cli) (built: Nov 23 2011 23:08:40)Copyright (c) 1997-2011 The PHP GroupZend Engine v2.4.0, ...
php > echo “Hello Worldn”;Hello Worldphp > print_r(array_map(function($x){ return $x * 2; },range(0,9)));Array(    [0] => ...
ARRAY SHORT SYNTAX
array(1, 2, 3)[1, 2, 3]
array(‘a’=>1, ‘b’=>2)[‘a’=>1, ‘b’=>2]
@rskyhttps://wiki.php.net/rfc/ shortsyntaxforarrays
var $belongsTo = array(! User);var $hasMany = array(    Photo => array(        order => number    ));var $belongsTo = [! U...
$this->render(list, array(  posts => Post::find(array(     limit => Config::get(postsPerPage,       array(context=>blog, d...
$this->render(list, array(  posts => Post::find(array(     limit => Config::get(postsPerPage,       array(context=>blog, d...
$this->render(list, array(  posts => Post::find(array(     limit => Config::get(postsPerPage,       array(context=>blog, d...
$this->render(list, array(  posts => Post::find(array(     limit => Config::get(postsPerPage,       array(context=>blog, d...
ARRAY SHORT SYNTAX•                  →•• PHP•          Array
ARRAY SHORT SYNTAX•   PHP   array       YAML•   PHP
BUILT-IN SERVER
PHPWeb
% sapi/cli/php -S localhost:8080
•   Javascript Flash     file://       API        →                    PHP5.4•           Apache•                      PHP
.htaccess   mod_rewrite
PHP% sapi/cli/php -S localhost:8080 builtin-server.php
list($path, $param) = array_merge(     preg_split(/?/, $_SERVER[REQUEST_URI], 2), [, ]);if($path != / && (file_exists(app/...
% ~/php54/php-5.4.0RC1/sapi/cli/php -S localhost:8080builtin-server.php
PHP 5.4.0RC1 Development Server started at Thu Nov 24 02:11:37 2011Listening on localhost:8080Document root is /Users/tana...
MacPorts       MySQL                      php.ini % ~/php54/php-5.4.0RC1/ sapi/cli/php -c ~/php54/ php-5.4.0RC1/ -S localh...
BUILT-IN SERVER•                                           URL                              OK• Apache•                 PH...
TRAIT
NO.1
TRAIT••   Ruby         mixin•          PHP               (instanceof   )•
1         INCLUDE / REQUIRE•• HTML•                          /
2•••   class AppModel extends Model    class GuestUser extends AppModel    class AdminUser extends AppModel               ...
class AppModel extends Model {}class GuestUser extends AppModel {    public function getDisplayLabel() {        ...;    }}...
class AppModel extends Model {    public function getDisplayLabel() {        ...;    }}class GuestUser extends AppModel {}...
class AppModel extends Model {    public function getDisplayLabel() {        return $this->username . “   ”;    }}class Gu...
UserModel.inc    public function getDisplayLabel() {        return $this->username . “   ”;    }class GuestUser extends Ap...
trait UserModel {    public function getDisplayLabel() {        return $this->username . “   ”;    }}class GuestUser exten...
trait PersistentModel {    public function save() {    }                                            =}abstract class User ...
PHP5.4O/R
CLOSURE
CALLABLE
call_user_funcphp   > $fun = intval;php   > echo call_user_func($fun, "0001abc"), "n";1php   > echo is_callable($fun), "n"...
call_user_func                             arrayphp >   $obj = new Exception(hoge);php >   $msg = [$obj, getMessage];php >...
php > echo $closure(), "n";   // string array                               ↓ is_callable                                 ...
$THIS IN CLOSURE
PHP5.3<?phpclass CounterFactory {    function __construct($init) {        $this->init = $init;    }    function createCoun...
PHP5.3<?phpclass CounterFactory {    function __construct($init) {        $this->init = $init;    }    function createCoun...
self → Python
PHP5.4                               OK!<?phpclass CounterFactory {    function __construct($init) {        $this->init = ...
<?phpclass CounterFactory {    function __construct($init) {        $this->init = $init;    }    function createCounter() ...
Javascript
$THIS<?phpclass CounterFactory {    function __construct($init) {        $this->init = $init;    }    function createCount...
<?phpclass CounterFactory {    function __construct($init) {        $this->init = $init;    }    function createCounter() ...
CLOSURE•   PHP5.4                       create_function•   Javascript           this•                    $this            ...
<?php                             <?phpclass Hoge {                      class Hoge {  function __construct($init) {     f...
• new•       array
PHP5.3                PHP5.4                  OKecho (new Exception("hoge"))->getMessage(), "n";
PHP5.3                PHP5.4       OKecho range(0, 9)[5], “n”;
PHP5.4.0RC1php > echo (array(1, 2, 3))[0], "n";Parse error: syntax error, unexpected [, expecting , or ;in php shell code ...
<?=<?=
PHP5.4• PHP5.3           5.2 • • Phar• PHP5.4
PHP5.4
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
Upcoming SlideShare
Loading in …5
×

関西PHP勉強会 php5.4つまみぐい

6,895 views
6,824 views

Published on

2011/11/24の関西PHP勉強会で発表したPHP5.4ネタです。

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
  • Very interesting hope to view further .
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
6,895
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
9
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

関西PHP勉強会 php5.4つまみぐい

  1. 1. PHP5.4@tanakahisateru
  2. 2. ABOUT ME• ( )• @tanakahisateru• https://github.com/tanakahisateru• Firebug, FireCookie, jEdit• ...and Pinoco
  3. 3. • PHP5.4• Array Short Syntax• Built-in Server• Trait• Closure•
  4. 4. :
  5. 5. PHP5.4Graham ( @predominant ) :) http://tipshare.info/view/ 4ec326d04b2122ce49000000
  6. 6. PHP5.4
  7. 7. PHP 5.4 RC1 (2011/11/11)• http://www.php.net/archive/2011.php#id2011-11-11-1
  8. 8. PHP PHP 5.4 http://www.1x1.jp/blog/ 2011/06/try_new_php_without_update _current_version.html configure make
  9. 9. • Windows • http://windows.php.net/qa/• Mac • XCode + MacPorts or Homebrew• Linux •
  10. 10. % curl -o php-5.4.0RC1.tar.gz http://downloads.php.net/stas/php-5.4.0RC1.tar.gz% tar xzf php-5.4.0RC1.tar.gz% cd php-5.4.0RC1% ./configure --prefix=/opt/local/php/5.4 --bindir=/opt/local/bin --with-config-file-path=/opt/local/php/5.4/etc --with-config-file-scan-dir=/opt/local/php/5.4/var/db --mandir=/opt/local/php/5.4/share/man --infodir=/opt/local/php/5.4/share/info --program-suffix=-5.4 --with-apxs2=/opt/local/apache2/bin/apxs ( https://gist.github.com/1344162 )% make
  11. 11. (make install)sapi/cli/php configure PHP
  12. 12. % sapi/cli/php -vPHP 5.4.0RC1 (cli) (built: Nov 23 2011 23:08:40)Copyright (c) 1997-2011 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2011 ZendTechnologies% sapi/cli/php -aInteractive shellphp >
  13. 13. php > echo “Hello Worldn”;Hello Worldphp > print_r(array_map(function($x){ return $x * 2; },range(0,9)));Array( [0] => 0 [1] => 2 [2] => 4 [3] => 6 [4] => 8 [5] => 10 [6] => 12 [7] => 14 [8] => 16 [9] => 18) :php > echo 0xff == 0b11111111, "n";1
  14. 14. ARRAY SHORT SYNTAX
  15. 15. array(1, 2, 3)[1, 2, 3]
  16. 16. array(‘a’=>1, ‘b’=>2)[‘a’=>1, ‘b’=>2]
  17. 17. @rskyhttps://wiki.php.net/rfc/ shortsyntaxforarrays
  18. 18. var $belongsTo = array(! User);var $hasMany = array( Photo => array( order => number ));var $belongsTo = [! User];var $hasMany = [ Photo => [ order => number ]];
  19. 19. $this->render(list, array( posts => Post::find(array( limit => Config::get(postsPerPage, array(context=>blog, default=>10)), )))); ?$this->render(list, [ posts => Post::find([ limit => Config::get(postsPerPage, [context=>blog, default=>10]), ])]);
  20. 20. $this->render(list, array( posts => Post::find(array( limit => Config::get(postsPerPage, array(context=>blog, default=>10)), ))));$this->render(list, [ posts => Post::find([ limit => Config::get(postsPerPage, [context=>blog, default=>10]), ])]);
  21. 21. $this->render(list, array( posts => Post::find(array( limit => Config::get(postsPerPage, array(context=>blog, default=>10)), ))));$this->render(list, [ posts => Post::find([ limit => Config::get(postsPerPage, [context=>blog, default=>10]), ])]); Array
  22. 22. $this->render(list, array( posts => Post::find(array( limit => Config::get(postsPerPage, array(context=>blog, default=>10)), ))));$this->render(list, [ posts => Post::find([ limit => Config::get(postsPerPage, [context=>blog, default=>10]), ])]); [ ... ]
  23. 23. ARRAY SHORT SYNTAX• →•• PHP• Array
  24. 24. ARRAY SHORT SYNTAX• PHP array YAML• PHP
  25. 25. BUILT-IN SERVER
  26. 26. PHPWeb
  27. 27. % sapi/cli/php -S localhost:8080
  28. 28. • Javascript Flash file:// API → PHP5.4• Apache• PHP
  29. 29. .htaccess mod_rewrite
  30. 30. PHP% sapi/cli/php -S localhost:8080 builtin-server.php
  31. 31. list($path, $param) = array_merge( preg_split(/?/, $_SERVER[REQUEST_URI], 2), [, ]);if($path != / && (file_exists(app/webroot . $path))){ header(sprintf(Location: http://%s/app/webroot%s, $_SERVER[HTTP_HOST], $_SERVER[REQUEST_URI])); exit;}else if($path != / && (file_exists(./ . $path))){ return false;}else{ $_SERVER[PATH_INFO] = $path; require app/webroot/index.php;}
  32. 32. % ~/php54/php-5.4.0RC1/sapi/cli/php -S localhost:8080builtin-server.php
  33. 33. PHP 5.4.0RC1 Development Server started at Thu Nov 24 02:11:37 2011Listening on localhost:8080Document root is /Users/tanakahisateru/Sites/cakephp2Press Ctrl-C to quit.[Thu Nov 24 02:11:42 2011] ::1:63556 [200]: /app/webroot/css/cake.generic.css[Thu Nov 24 02:11:42 2011] ::1:63557 [200]: /app/webroot/img/cake.power.gif[Thu Nov 24 02:11:42 2011] ::1:63558 [200]: /app/webroot/img/cake.icon.png[Thu Nov 24 02:11:42 2011] ::1:63564 [200]: /app/webroot/favicon.ico
  34. 34. MacPorts MySQL php.ini % ~/php54/php-5.4.0RC1/ sapi/cli/php -c ~/php54/ php-5.4.0RC1/ -S localhost:8080 builtin-server.php ~/php54/php-5.4.0RC1/php.ini[Pdo_mysql]pdo_mysql.default_socket=/opt/local/var/run/mysql5/mysqld.sock MySQL
  35. 35. BUILT-IN SERVER• URL OK• Apache• PHP5.3 Apache mod_php PHP5.4
  36. 36. TRAIT
  37. 37. NO.1
  38. 38. TRAIT•• Ruby mixin• PHP (instanceof )•
  39. 39. 1 INCLUDE / REQUIRE•• HTML• /
  40. 40. 2••• class AppModel extends Model class GuestUser extends AppModel class AdminUser extends AppModel User
  41. 41. class AppModel extends Model {}class GuestUser extends AppModel { public function getDisplayLabel() { ...; }} !!class AdminUser extends AppModel { public function getDisplayLabel() { ...; } public function getAdminRioleType() { ...; }}
  42. 42. class AppModel extends Model { public function getDisplayLabel() { ...; }}class GuestUser extends AppModel {}class AdminUser extends AppModel { public function getAdminRioleType() { ...; }}
  43. 43. class AppModel extends Model { public function getDisplayLabel() { return $this->username . “ ”; }}class GuestUser extends AppModel {}class AdminUser extends AppModel { public function getAdminRoleType() { ...; }}class Comment extends AppModel { // username ←}
  44. 44. UserModel.inc public function getDisplayLabel() { return $this->username . “ ”; }class GuestUser extends AppModel { require ‘UserModel.inc’;}class AdminUser extends AppModel { require ‘UserModel.inc’; public function getAdminRoleType() { ...; }}class Comment extends AppModel {} ... require = orz
  45. 45. trait UserModel { public function getDisplayLabel() { return $this->username . “ ”; }}class GuestUser extends AppModel { use UserModel;}class AdminUser extends AppModel { use UserModel; public function getAdminRoleType() { ...; }}class Comment extends AppModel {}
  46. 46. trait PersistentModel { public function save() { } =}abstract class User { = public function getDisplayLabel() { return $this->username . “ ”; }}class GuestUser extends User inplements Persistence { use PersistentModel;}class AdminUser extends User inplements Persistence { use PersistentModel; public function getAdminRoleType() { ...; }}
  47. 47. PHP5.4O/R
  48. 48. CLOSURE
  49. 49. CALLABLE
  50. 50. call_user_funcphp > $fun = intval;php > echo call_user_func($fun, "0001abc"), "n";1php > echo is_callable($fun), "n";1php > echo is_string($fun), "n";1php > echo $fun("0001abc"), "n";1
  51. 51. call_user_func arrayphp > $obj = new Exception(hoge);php > $msg = [$obj, getMessage];php > echo call_user_func($msg), "n";hogephp > echo is_callable($msg), "n";1php > echo is_array($msg), "n";1php > echo $msg(), "n";hoge
  52. 52. php > echo $closure(), "n"; // string array ↓ is_callable Array
  53. 53. $THIS IN CLOSURE
  54. 54. PHP5.3<?phpclass CounterFactory { function __construct($init) { $this->init = $init; } function createCounter() { $c = 0; return function() use(&$c) { return $this->init + ($c++); }; }}$fact = new CounterFactory(100);$c1 = $fact->createCounter();$c2 = $fact->createCounter();echo "c1:", $c1(), "n"; echo "c1:", $c1(), "n";echo "c2:", $c2(), "n"; echo "c2:", $c2(), "n";echo "c2:", $c2(), "n"; echo "c1:", $c1(), "n";
  55. 55. PHP5.3<?phpclass CounterFactory { function __construct($init) { $this->init = $init; } function createCounter() { $c = 0; $self = $this; return function() use($self, &$c) { return $self->init + ($c++); }; }}$fact = new CounterFactory(100);$c1 = $fact->createCounter();$c2 = $fact->createCounter();echo "c1:", $c1(), "n"; echo "c1:", $c1(), "n";echo "c2:", $c2(), "n"; echo "c2:", $c2(), "n";echo "c2:", $c2(), "n"; echo "c1:", $c1(), "n";
  56. 56. self → Python
  57. 57. PHP5.4 OK!<?phpclass CounterFactory { function __construct($init) { $this->init = $init; } function createCounter() { $c = 0; return function() use(&$c) { return $this->init + ($c++); }; }}$fact = new CounterFactory(100);$c1 = $fact->createCounter();$c2 = $fact->createCounter();echo "c1:", $c1(), "n"; echo "c1:", $c1(), "n";echo "c2:", $c2(), "n"; echo "c2:", $c2(), "n";echo "c2:", $c2(), "n"; echo "c1:", $c1(), "n";
  58. 58. <?phpclass CounterFactory { function __construct($init) { $this->init = $init; } function createCounter() { $c = 0; return function() use(&$c) { return $this->init + ($c++); }; }}$fact = new CounterFactory(100);$c1 = $fact->createCounter();$c2 = $fact->createCounter();echo "c1:", $c1(), "n"; echo "c1:", $c1(), "n";echo "c2:", $c2(), "n"; echo "c2:", $c2(), "n";echo "c2:", $c2(), "n"; echo "c1:", $c1(), "n";c1:100 <-- init=100 + c=0c1:101 <-- init=100 + c=1c2:100 <-- init=100 + c=0c2:101 <-- init=100 + c=1c2:102 <-- init=100 + c=2c1:102 <-- init=100 + c=2
  59. 59. Javascript
  60. 60. $THIS<?phpclass CounterFactory { function __construct($init) { $this->init = $init; } function createCounter() { $c = 0; return function() use(&$c) { return $this->init + ($c++); }; }}$fact = new CounterFactory(100);$c1 = $fact->createCounter();echo "c1:", $c1(), "n";echo "c1:", $c1(), "n";$c1 = $c1->bindTo(new CounterFactory(1000));echo "c1:", $c1(), "n";
  61. 61. <?phpclass CounterFactory { function __construct($init) { $this->init = $init; } function createCounter() { $c = 0; return function() use(&$c) { return $this->init + ($c++); }; }}$fact = new CounterFactory(100);$c1 = $fact->createCounter();echo "c1:", $c1(), "n";echo "c1:", $c1(), "n";$c1 = $c1->bindTo(new CounterFactory(1000));echo "c1:", $c1(), "n";c1:100 <-- init=100 + c=0c1:101 <-- init=100 + c=1c1:1002 <-- init=1000 + c=2
  62. 62. CLOSURE• PHP5.4 create_function• Javascript this• $this $this• → PHP5.3 $self bindTo PHP5.4 $this
  63. 63. <?php <?phpclass Hoge { class Hoge { function __construct($init) { function __construct($init) { $this->init = $init; $this->init = $init; } } function x($n) { function x($n) { $tmp = []; return array_map( for($i=0; $i<$n; $i++) { function() { $tmp[] = $this->init; return $this->init; } }, range(0,$n-1) return $tmp; ); } }} }print_r((new Hoge(100))->x(3)); print_r((new Hoge(100))->x(3));
  64. 64. • new• array
  65. 65. PHP5.3 PHP5.4 OKecho (new Exception("hoge"))->getMessage(), "n";
  66. 66. PHP5.3 PHP5.4 OKecho range(0, 9)[5], “n”;
  67. 67. PHP5.4.0RC1php > echo (array(1, 2, 3))[0], "n";Parse error: syntax error, unexpected [, expecting , or ;in php shell code on line 1php > echo (function($x){ return $x * 2; })(10), "n";Parse error: syntax error, unexpected (, expecting , or ;in php shell code on line 1
  68. 68. <?=<?=
  69. 69. PHP5.4• PHP5.3 5.2 • • Phar• PHP5.4
  70. 70. PHP5.4

×