たんたんめん日記

ソシャゲ関連のなんでもやさん備忘録

今更ながらPHP5.4とPHP5.5の変更点をまとめた

しばらくPHPから遠ざかっていたんですが、久しぶりにPHPを使うことになりました。
どうせなら最新のPHP5.5を使ってみようかと思うんですが、なんせ脳内は5.3で停止している…

ということで、恐ろしく今更ながら5.4、5.5の変更点をまとめてみました。
自分のコーディングに関係ありそうなところのみですが…

PHP5.4

変更点

  • traitの追加

  • 配列の短縮構文

$a = [1,2,3];
  • 関数の返り値を配列として扱える
function return_array(){
    return [1,2,3];
}

echo return_array()[1];
  • <?=が常に有効
    short_open_tagの設定の影響を受けない

  • クラスのインスタンス作成と同時にメンバにアクセス出来る

class Test{
    public function hello(){
        echo ‘hello';
    }
}

(new Test())->hello();
$ cd ~/public_html
$ php -S localhost:8000
  • PHP-FPM: 設定項目 process.max を追加
    nginx+php-fpm実行時のお供に。

  • debug_backtracedebug_print_backtrace にlimitオプションが追加
    返すスタックフレームの量を制限できる。デバッグ時のお供に。

  • hex2bin 関数の追加
    未だにpack関数は良く分からない。

  • http_response_code 関数の追加
    HTTP レスポンスコードを取得または設定出来る。

http_response_code(404);
  • session_status 関数の追加
    現在のセッションの状態を返す。

  • MySQLのライブラリ
    MySQL 用の拡張モジュールである mysql や mysqli 、 PDO_mysql が、デフォルトのライブラリとして mysqlnd を使うようになった。

  • $_SERVER['REQUEST_TIME_FLOAT’]の追加
    リクエスト時間がマイクロ秒単位で取得出来る。デバッグのお供に。

  • mbstring.script_encodingが削除
    zend.script_encodingを使用する

  • session_is_registered(), session_register() および session_unregister()の削除
    昔は良く使っていたような気がする。

PHP5.5

  • finally句の追加
try {
    Throw new Exception('例外発生');
}
catch( Exception $e ){
    echo $e->getMessage();
}
finally {
    echo 'ここは通りますよ’ ;
}
  • ジェネレータの追加
$test_array = [1,2,3,4,5];

function g_TestArray( $array ){
    foreach( $array as $each ){
        yield $each;
    }
}

foreach( g_TestArray($test_array) as  $test_val ){
    echo $test_val ;
}
<?php

class Test{}

echo Test::class;

markdownエディタをBywordに乗り換えた

markdownとわたし

最近、会社で作成するテキストのほとんどをmarkdownで作成しています。

具体的には

  • インストール等の手順書
  • 議事録
  • 提案書の案

という感じでしょうか。

簡単な文法で、きれいなフォーマットで出力出来るのが魅力ですね。
で、そのmarkdownを書くのに使っていたのがwri.peです。

素晴らしきwri.pe

wri.peは著名なエンジニアであるmasuidriveさんが作成されたwebサービスです。
動作が軽くサクサクと文章を作成することが出来ます。

個人的に必須なのがEvernoteへの自動出力機能です。
仕事・プライベートのあらゆるメモをEvernoteに詰め込んでいるので、自動で出力されるのはかなり使い勝手が良いわけです。

あまりに便利なので、fluidでアプリ化して、Dockからサクッと起動できるようにしていました。

が、しかしwri.peの使用を止めました。

Bywordに乗り換えた

なぜwri.peを止めたかというと、あまりに便利過ぎてこれ無しでは仕事出来なくなってきていたので。
webなので、サービスが終了すると終わりなわけです。 それは、やばいと。。

というわけで、Macのmarkdownエディタを探して、行き着いたのがBywordです。

Byword、ええですね

Bywordをなぜ選んだかというと、単純に、Evernote への出力機能(有料オプション)が利用可能だから。

しかし、これが自動では無いんですね…
まあ、この点はちょっと目をつぶるとして、全体的にシンプルなUIと、iCloudへの保存に対応しているのが好印象です。

アプリ代1000円+Evernote等への出力オプション500円の計1500円ですが、今のところその価値はあるかなと、と思います。

【PHP】Hip-Hop vm を試してみた

https://www.facebook.com/notes/facebook-engineering/the-hiphop-virtual-machine/10150415177928920 の記事を発見。

「そういや、そんなんあったなー。」 と、思いながら、リンクを辿って行くと

https://github.com/facebook/hiphop-php/wiki/HHVM-Pre-built-packages-for-Centos-6.4
CentOSへのインストール方法の解説が。

なんか、案外簡単に試すことが出来そうです。
ということで、vagrantに環境作って動かしてみました。

ひとまず、wordpressを動かしてみようじゃないかということで、 こちらを参考にさせて頂き、作業してみました。


HipHopのインストール

上のCentOSへのインストール方法そのままにインストール。

# yum install http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm

# cd /etc/yum.repos.d

# wget http://www.hop5.in/yum/el6/hop5.repo

mysqlが5.1とか入っちゃうので、remiから5.5をインストール。気分的な問題かも。

# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# yum install -y perl-DBD-MySQL libaio libaio-devel openssl-devel
# yum --enablerepo=remi --disablerepo=updates,base install mysql mysql-server mysql-devel

WordPress設置

ずぼらにrootで放り込みます。

# mkdir /var/www
# cd /var/www
# wget http://ja.wordpress.org/wordpress-3.6.1-ja.tar.gz
# tar xvzf wordpress-3.6.1-ja.tar.gz 

# cd wordpress
# cp wp-config-sample.php wp-config.php 

データベース設定

設定前にmysqlの初期化、DBの作成と、ユーザーの追加を行っておきます。

vim /var/www/wordpress/wp-config.php

define('DB_NAME', 'wpdata');

define('DB_USER', 'dbmanager');

define('DB_PASSWORD', 'dbmanagerpass');


hhvmの設定

設定ファイルを新規作成。こちらの参考記事を元に作成しました。

vim /etc/hhvm.hdf

Server {
  Port = 80
  SourceRoot = /var/www/wordpress
}


VirtualHost {
  * {
    Pattern = .*
    RewriteRules {
      dirindex {
        pattern = ^/(.*)/*$
        to = $1/index.php
        qsa = true
      }
    }
  }
}

StaticFile {
  Extensions {
    css = text/css
    gif = image/gif
    html = text/html
    jpe = image/jpeg
    jpeg = image/jpeg
    jpg = image/jpeg
    png = image/png
    tif = image/tiff
    tiff = image/tiff
    txt = text/plain
  }
}

パスの正規表現がイケてないな…


サーバー起動

# hhvm --mode  daemon --config /etc/hhvm.hdf

これでWebサーバーも立ち上がっているので、/wp-admin/install.phpにアクセスしてwordpressの初期設定を行っておきます。


比較用のapache/phpをインストール

ベンチマーク取ってみたいので、比較用にapache環境もインストール

# yum install -y  pcre-devel
# yum install httpd php php-devel php-pear php-mysql 

インストールされたものは↓な感じです

# yum list httpd php php-devel php-pear php-mysql 
httpd.x86_64                                    2.2.15-29.el6.centos                                 @updates
php.x86_64                                      5.3.3-23.el6_4                                       @updates
php-devel.x86_64                                5.3.3-23.el6_4                                       @updates
php-mysql.x86_64                                5.3.3-23.el6_4                                       @updates
php-pear.noarch                                 1:1.9.4-4.el6                                        @base   

apcもインストール

# pecl install APC

httpd.confを編集

vim /etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/html"
↓
DocumentRoot "/var/www/wordpress"

php.iniを編集

vim /etc/php.ini

;date.timezone = 
↓
date.timezone = Asia/Tokyo

追加
↓
extension=apc.so

負荷テスト

手元のMBAからabで。

apapche編

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.xx.xx (be patient).....done


Server Software:        Apache/2.2.15
Server Hostname:        192.168.xx.xx
Server Port:            80

Document Path:          /?p=5
Document Length:        11297 bytes

Concurrency Level:      10
Time taken for tests:   6.220 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      1151400 bytes
HTML transferred:       1129700 bytes
Requests per second:    16.08 [#/sec] (mean)
Time per request:       622.008 [ms] (mean)
Time per request:       62.201 [ms] (mean, across all concurrent requests)
Transfer rate:          180.77 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:   365  615  56.7    612     791
Waiting:      353  487  42.5    481     652
Total:        365  615  56.7    613     791

Percentage of the requests served within a certain time (ms)
  50%    613
  66%    633
  75%    644
  80%    652
  90%    677
  95%    711
  98%    760
  99%    791
 100%    791 (longest request)


hiphop編

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.xx.xx (be patient).....done


Server Software:        
Server Hostname:        192.168.xx.xx
Server Port:            80

Document Path:          /?p=5
Document Length:        11297 bytes

Concurrency Level:      10
Time taken for tests:   2.237 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      1144400 bytes
HTML transferred:       1129700 bytes
Requests per second:    44.69 [#/sec] (mean)
Time per request:       223.748 [ms] (mean)
Time per request:       22.375 [ms] (mean, across all concurrent requests)
Transfer rate:          499.48 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:   104  221  32.1    225     288
Waiting:      102  210  32.2    211     280
Total:        104  221  32.1    226     288

Percentage of the requests served within a certain time (ms)
  50%    226
  66%    236
  75%    240
  80%    242
  90%    260
  95%    269
  98%    280
  99%    288
 100%    288 (longest request)

3倍近く? apacheのチューニングを全くしていないからかもですが、結構差が出ました。

参考サイトでは、メディアのアップロードが出来ないとのことでしたが、 試してみたところ問題なくアップロード出来ました。
修正されたんでしょうかね、、、


業務で使うには、まだまだ怖いですが、このパフォーマンスはなかなかに魅力的です!