関西PHP勉強会 php5.4つまみぐい
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 6,577 views

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

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

Statistics

Views

Total Views
6,577
Views on SlideShare
5,399
Embed Views
1,178

Actions

Likes
4
Downloads
8
Comments
1

8 Embeds 1,178

http://masutaka.net 916
http://www.kphpug.jp 201
http://paper.li 20
http://coderwall.com 19
http://webcache.googleusercontent.com 12
http://a0.twimg.com 6
http://localhost 3
http://www.mefeedia.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Very interesting hope to view further .
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

    • PHP5.4@tanakahisateru
    • ABOUT ME• ( )• @tanakahisateru• https://github.com/tanakahisateru• Firebug, FireCookie, jEdit• ...and Pinoco
    • • PHP5.4• Array Short Syntax• Built-in Server• Trait• Closure•
    • :
    • PHP5.4Graham ( @predominant ) :) http://tipshare.info/view/ 4ec326d04b2122ce49000000
    • 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 configure make
    • • 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.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
    • (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, Copyright (c) 1998-2011 ZendTechnologies% sapi/cli/php -aInteractive shellphp >
    • 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
    • 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 = [! User];var $hasMany = [ Photo => [ order => number ]];
    • $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]), ])]);
    • $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]), ])]);
    • $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
    • $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 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/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;}
    • % ~/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/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
    • 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
    • BUILT-IN SERVER• URL OK• Apache• PHP5.3 Apache mod_php PHP5.4
    • 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 User
    • class AppModel extends Model {}class GuestUser extends AppModel { public function getDisplayLabel() { ...; }} !!class AdminUser extends AppModel { public function getDisplayLabel() { ...; } public function getAdminRioleType() { ...; }}
    • class AppModel extends Model { public function getDisplayLabel() { ...; }}class GuestUser extends AppModel {}class AdminUser extends AppModel { public function getAdminRioleType() { ...; }}
    • 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 ←}
    • 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
    • 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 {}
    • 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() { ...; }}
    • PHP5.4O/R
    • CLOSURE
    • CALLABLE
    • 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
    • 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
    • php > echo $closure(), "n"; // string array ↓ is_callable Array
    • $THIS IN CLOSURE
    • 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";
    • 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";
    • self → Python
    • 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";
    • <?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
    • Javascript
    • $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";
    • <?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
    • CLOSURE• PHP5.4 create_function• Javascript this• $this $this• → PHP5.3 $self bindTo PHP5.4 $this
    • <?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));
    • • 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 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
    • <?=<?=
    • PHP5.4• PHP5.3 5.2 • • Phar• PHP5.4
    • PHP5.4