Contenu connexe Similaire à 関西PHP勉強会 php5.4つまみぐい (20) Plus de Hisateru Tanaka (19) 関西PHP勉強会 php5.4つまみぐい2. ABOUT ME
• ( )
• @tanakahisateru
• https://github.com/tanakahisateru
• Firebug, FireCookie, jEdit
• ...and Pinoco
4. • PHP5.4
• Array Short Syntax
• Built-in Server
• Trait
• Closure
•
6. PHP5.4
Graham ( @predominant )
:)
http://tipshare.info/view/
4ec326d04b2122ce49000000
8. PHP 5.4 RC1 (2011/11/11)
• http://www.php.net/archive/2011.php#id2011-11-11-1
9. PHP
PHP 5.4
http://www.1x1.jp/blog/
2011/06/
try_new_php_without_update
_current_version.html
configure make
10. • Windows
• http://windows.php.net/qa/
• Mac
• XCode + MacPorts or Homebrew
• Linux
•
11. % 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
13. % sapi/cli/php -v
PHP 5.4.0RC1 (cli) (built: Nov 23 2011 23:08:40)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2011 Zend
Technologies
% sapi/cli/php -a
Interactive shell
php >
14. php > echo “Hello Worldn”;
Hello World
php > 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
19. var $belongsTo = array(
! 'User'
);
var $hasMany = array(
'Photo' => array(
'order' => 'number'
)
);
var $belongsTo = [
! 'User'
];
var $hasMany = [
'Photo' => [
'order' => 'number'
]
];
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]),
])
]);
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. $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
24. $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]),
])
]);
[ ... ]
30. • Javascript Flash file:// API
→ PHP5.4
• Apache
• PHP
33. 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';
}
36. PHP 5.4.0RC1 Development Server started at Thu Nov 24 02:11:37 2011
Listening on localhost:8080
Document root is /Users/tanakahisateru/Sites/cakephp2
Press 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
37. 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
41. TRAIT
•
• Ruby mixin
• PHP (instanceof )
•
43. 1
INCLUDE / REQUIRE
•
• HTML
• /
44. 2
•
•
• class AppModel extends Model
class GuestUser extends AppModel
class AdminUser extends AppModel
User
45. class AppModel extends Model {
}
class GuestUser extends AppModel {
public function getDisplayLabel() {
...;
}
} !!
class AdminUser extends AppModel {
public function getDisplayLabel() {
...;
}
public function getAdminRioleType() {
...;
}
}
46. class AppModel extends Model {
public function getDisplayLabel() {
...;
}
}
class GuestUser extends AppModel {
}
class AdminUser extends AppModel {
public function getAdminRioleType() {
...;
}
}
47. 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 ←
}
48. 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
49. 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 {
}
51. 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() {
...;
}
}
57. call_user_func
php > $fun = 'intval';
php > echo call_user_func($fun, "0001abc"), "n";
1
php > echo is_callable($fun), "n";
1
php > echo is_string($fun), "n";
1
php > echo $fun("0001abc"), "n";
1
58. call_user_func array
php > $obj = new Exception('hoge');
php > $msg = [$obj, 'getMessage'];
php > echo call_user_func($msg), "n";
hoge
php > echo is_callable($msg), "n";
1
php > echo is_array($msg), "n";
1
php > echo $msg(), "n";
hoge
59. php > echo $closure(), "n"; //
string array
↓
is_callable
Array
61. PHP5.3
<?php
class 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";
62. PHP5.3
<?php
class 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";
64. PHP5.4 OK!
<?php
class 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";
65. <?php
class 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=0
c1:101 <-- init=100 + c=1
c2:100 <-- init=100 + c=0
c2:101 <-- init=100 + c=1
c2:102 <-- init=100 + c=2
c1:102 <-- init=100 + c=2
67. $THIS
<?php
class 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";
68. <?php
class 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=0
c1:101 <-- init=100 + c=1
c1:1002 <-- init=1000 + c=2
69. CLOSURE
• PHP5.4 create_function
• Javascript this
• $this $this
•
→ PHP5.3 $self bindTo PHP5.4
$this
70. <?php <?php
class 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));
74. PHP5.3
PHP5.4 OK
echo (new Exception("hoge"))->getMessage(), "n";
75. PHP5.3
PHP5.4 OK
echo range(0, 9)[5], “n”;
76. PHP5.4.0RC1
php > echo (array(1, 2, 3))[0], "n";
Parse error: syntax error, unexpected '[', expecting ',' or ';'
in php shell code on line 1
php > echo (function($x){ return $x * 2; })(10), "n";
Parse error: syntax error, unexpected '(', expecting ',' or ';'
in php shell code on line 1