いまどきのphp
DESCRIPTION
2013/09/28 関西PHP勉強会 in JAWS FESTA Kansai 2013TRANSCRIPT
2013/09/28 shin1x1
関西PHP勉強会 in 京セラドーム
いまどきのPHP
昔のPHP
(c) 2013 Masashi Shinbara @shin1x1
• HTMLとPHPの混在• <body>タグの中にロジック• どこからでもDB接続• 重複しまくりコード• とびかうグローバル変数....
昔のPHP
(c) 2013 Masashi Shinbara @shin1x1
HTMLとPHPが混在したコード
Agenda
(c) 2013 Masashi Shinbara @shin1x1
• オブジェクト指向機能• オブジェクト指向の活用• 最近のトピック
(c) 2013 Masashi Shinbara @shin1x1
オブジェクト指向機能
オブジェクト指向言語
(c) 2013 Masashi Shinbara @shin1x1
• 隠蔽(カプセル化)• 継承(インヘリタンス)• 多態性(ポリモフィズム)
隠蔽(カプセル化)
(c) 2013 Masashi Shinbara @shin1x1
• アクセス修飾子<?phpclass Foo{ public $public = 'public'; protected $protected = 'protected'; private $private = 'private';}
public どこからでもアクセス可能protected 定義したクラス、継承したクラスprivate 定義したクラスのみ
隠蔽(カプセル化)
(c) 2013 Masashi Shinbara @shin1x1
<?phpclass Foo{ public function public_method() { // どこからでも実行可 }
protected function protected_method() { // Fooクラスとその継承クラス }
private function private_method() { // Fooクラスのみ }}
継承
(c) 2013 Masashi Shinbara @shin1x1
<?phpclass Foo{ public function hello() { echo 'Foo'.PHP_EOL; }}
class FooChild extends Foo{ public function hello() { echo 'FooChild'.PHP_EOL; }}
$obj = new FooChild();$obj->hello(); // FooChild
• 単一継承のみ
継承
多態性(ポリモフィズム)
(c) 2013 Masashi Shinbara @shin1x1
<?phpinterface Printable{ public function output();}
class Foo implements Printable{ public function output() { echo 'Foo'; }}
class Bar implements Printable{ public function output() { echo 'Bar'; }}
多態性(ポリモフィズム)
(c) 2013 Masashi Shinbara @shin1x1
<?phpclass SomeObject{ public function execute(Printable $obj) { $obj->output(); }}
$obj = new SomeObject();$obj->execute(new Foo()); // Foo$obj->execute(new Bar()); // Bar
Printable なら ok
クラスメソッド、定数、変数
(c) 2013 Masashi Shinbara @shin1x1
<?phpclass Foo{ const CLASS_CONSTATNS = 'aws'; // クラス定数 static public $classValue = 'abc'; // クラス変数
static public function classMethod() { // クラスメソッド }}
echo FOO::CLASS_CONSTATNS; // awsecho Foo::$classValue; // abcFoo::classMethod();
コンストラクタ、デストラクタ
(c) 2013 Masashi Shinbara @shin1x1
<?phpclass Foo{ protected $name = null;
public function __construct($name) { $this->name = $name; echo '__construct'.PHP_EOL; }
public function __destruct() { echo '__destruct'.PHP_EOL; }}
$obj = new Foo('Jun'); // __construct// __destruct
コンストラクタ
デストラクタ
抽象クラス、メソッド
(c) 2013 Masashi Shinbara @shin1x1
<?phpabstract class AbstractFoo { public function something() { $this->hello(); }
abstract protected function hello();}
class Foo extends AbstractFoo{ protected function hello() { echo 'Foo'; }}
$obj = new Foo();$obj->something(); // Foo
抽象クラス(インスタンス化不可)
具象クラス
final
(c) 2013 Masashi Shinbara @shin1x1
<?phpfinal class NoInheritance{}
class Foo{ final public function noInheritanceMethod() { // }}
継承できないオーバーライドできない
インターフェイス
(c) 2013 Masashi Shinbara @shin1x1
<?phpinterface Printable{ public function printValue($value);}
interface Writable{ public function writeValue($value);}
class Foo implements Printable, Writable{ public function printValue($value) { // }
public function writeValue($value) { // }}
複数実装可
タイプヒンティング
(c) 2013 Masashi Shinbara @shin1x1
<?phpinterface Printable {}interface Writable {}
class Foo{ public function bar(Printable $printer, Writable $writer) { // }
public function something(array $array) { // }}
タイプヒンティング
タイプヒンティング
タイプヒンティング
名前空間
(c) 2013 Masashi Shinbara @shin1x1
<?phpnamespace Vendor\lib;
use Aws\S3\S3Client;
class Foo{ public function method() { throw new \Exception(); }}
名前空間の宣言
名前空間のインポート
グローバル
Vendor\lib\Foo
トレイト
(c) 2013 Masashi Shinbara @shin1x1
• Mixin のようなもの • 変数やメソッドをまとめたもの• クラスに追加して利用• 複数個追加できる
トレイト
(c) 2013 Masashi Shinbara @shin1x1
<?phptrait Say{ protected $name = 'Jun';
public function say() { echo 'Hello '.$this->name.PHP_EOL; }}
class Foo{ use Say;}
$obj = new Foo();$obj->say();
トレイト
トレイトの利用
PHPのオブジェクト指向機能
(c) 2013 Masashi Shinbara @shin1x1
詳しくは
(c) 2013 Masashi Shinbara @shin1x1
http://jp1.php.net/manual/ja/language.oop5.php
(c) 2013 Masashi Shinbara @shin1x1
オブジェクト指向の活用
フレームワーク
(c) 2013 Masashi Shinbara @shin1x1
• フレームワークの利用• オブジェクト指向• MVC• デファクトスタンダードは無い
フレームワーク
(c) 2013 Masashi Shinbara @shin1x1
• CakePHP
• Symfony
• Zend Framework
• CodeIgniter
• FuelPHP
• Laravel
• Sliex
• Phalcon
• Ethna
• Yii
• BEAR.Sunday
• Lithium
• TYPO3 FLOW
• Kohana
• Slim
• Aura for PHP
Symfony
(c) 2013 Masashi Shinbara @shin1x1
• コンポーネントで構成• コンポーネントは独立して利用可• 他のフレームワークやライブラリで• 部品としての品質も高い• Zend Frameworkも
PHPUnit
(c) 2013 Masashi Shinbara @shin1x1
http://phpunit.de/manual/3.7/ja/
PHPUnit
(c) 2013 Masashi Shinbara @shin1x1
• xUnit の PHP 実装• テストダブルやSelenium連携など• フレームワークやライブラリのテスト
PhpStorm
(c) 2013 Masashi Shinbara @shin1x1
http://www.jetbrains.com/phpstorm/
PhpStorm
(c) 2013 Masashi Shinbara @shin1x1
• JetBRAINS社のIDE• 補完機能が強力• オブジェクト指向に則れば恩恵大• いま注目のIDE
(c) 2013 Masashi Shinbara @shin1x1
最近のトピック
PHP 5.5 リリース
(c) 2013 Masashi Shinbara @shin1x1
• 2013/06 リリース• ジェネレータ• OPcache• パスワードハッシュ関数
PHP 5.3 が EOL
(c) 2013 Masashi Shinbara @shin1x1
• 5.3.27 が最後のリリース• セキュリティフィックスのみ• 2014/06 頃に終了• 今後は、5.4 or 5.5 を利用
BEAR.Sunday
(c) 2013 Masashi Shinbara @shin1x1
https://code.google.com/p/bearsunday/
BEAR.Sunday
(c) 2013 Masashi Shinbara @shin1x1
• @koriym さんが開発• リソース指向のフレームワーク• phpnw で講演• Litinum 作者も注目
Ginq
(c) 2013 Masashi Shinbara @shin1x1
https://github.com/akanehara/ginq
Ginq
(c) 2013 Masashi Shinbara @shin1x1
• @a.kanehara さんが開発• LINQ to Object を PHP に• メソッドチェインで配列操作• 遅延実行
注目のキーワード
(c) 2013 Masashi Shinbara @shin1x1
• PhpStorm• Composer• PSR• Vagrant
(c) 2013 Masashi Shinbara @shin1x1
まとめ
まとめ
(c) 2013 Masashi Shinbara @shin1x1
• PHPでオブジェクト指向開発• PHPらしく貪欲に成長• どう使うかはあなた次第
@shin1x1
(c) 2013 Masashi Shinbara @shin1x1