いまどきのphp

41
2013/09/28 shin1x1 関西PHP勉強会 in 京セラドーム いまどきのPHP

Upload: masashi-shinbara

Post on 08-Sep-2014

11 views

Category:

Technology


0 download

DESCRIPTION

2013/09/28 関西PHP勉強会 in JAWS FESTA Kansai 2013

TRANSCRIPT

Page 1: いまどきのPHP

2013/09/28 shin1x1

関西PHP勉強会 in 京セラドーム

いまどきのPHP

Page 2: いまどきのPHP

昔のPHP

(c) 2013 Masashi Shinbara @shin1x1

• HTMLとPHPの混在• <body>タグの中にロジック• どこからでもDB接続• 重複しまくりコード• とびかうグローバル変数....

Page 3: いまどきのPHP

昔のPHP

(c) 2013 Masashi Shinbara @shin1x1

HTMLとPHPが混在したコード

Page 4: いまどきのPHP

Agenda

(c) 2013 Masashi Shinbara @shin1x1

• オブジェクト指向機能• オブジェクト指向の活用• 最近のトピック

Page 5: いまどきのPHP

(c) 2013 Masashi Shinbara @shin1x1

オブジェクト指向機能

Page 6: いまどきのPHP

オブジェクト指向言語

(c) 2013 Masashi Shinbara @shin1x1

• 隠蔽(カプセル化)• 継承(インヘリタンス)• 多態性(ポリモフィズム)

Page 7: いまどきのPHP

隠蔽(カプセル化)

(c) 2013 Masashi Shinbara @shin1x1

• アクセス修飾子<?phpclass Foo{ public $public = 'public'; protected $protected = 'protected'; private $private = 'private';}

public どこからでもアクセス可能protected 定義したクラス、継承したクラスprivate 定義したクラスのみ

Page 8: いまどきのPHP

隠蔽(カプセル化)

(c) 2013 Masashi Shinbara @shin1x1

<?phpclass Foo{ public function public_method() { // どこからでも実行可 }

protected function protected_method() { // Fooクラスとその継承クラス }

private function private_method() { // Fooクラスのみ }}

Page 9: いまどきのPHP

継承

(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

• 単一継承のみ

継承

Page 10: いまどきのPHP

多態性(ポリモフィズム)

(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'; }}

Page 11: いまどきのPHP

多態性(ポリモフィズム)

(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

Page 12: いまどきのPHP

クラスメソッド、定数、変数

(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();

Page 13: いまどきのPHP

コンストラクタ、デストラクタ

(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

コンストラクタ

デストラクタ

Page 14: いまどきのPHP

抽象クラス、メソッド

(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

抽象クラス(インスタンス化不可)

具象クラス

Page 15: いまどきのPHP

final

(c) 2013 Masashi Shinbara @shin1x1

<?phpfinal class NoInheritance{}

class Foo{ final public function noInheritanceMethod() { // }}

継承できないオーバーライドできない

Page 16: いまどきのPHP

インターフェイス

(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) { // }}

複数実装可

Page 17: いまどきのPHP

タイプヒンティング

(c) 2013 Masashi Shinbara @shin1x1

<?phpinterface Printable {}interface Writable {}

class Foo{ public function bar(Printable $printer, Writable $writer) { // }

public function something(array $array) { // }}

タイプヒンティング

タイプヒンティング

タイプヒンティング

Page 18: いまどきのPHP

名前空間

(c) 2013 Masashi Shinbara @shin1x1

<?phpnamespace Vendor\lib;

use Aws\S3\S3Client;

class Foo{ public function method() { throw new \Exception(); }}

名前空間の宣言

名前空間のインポート

グローバル

Vendor\lib\Foo

Page 19: いまどきのPHP

トレイト

(c) 2013 Masashi Shinbara @shin1x1

• Mixin のようなもの • 変数やメソッドをまとめたもの• クラスに追加して利用• 複数個追加できる

Page 20: いまどきのPHP

トレイト

(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();

トレイト

トレイトの利用

Page 21: いまどきのPHP

PHPのオブジェクト指向機能

(c) 2013 Masashi Shinbara @shin1x1

Page 22: いまどきのPHP

詳しくは

(c) 2013 Masashi Shinbara @shin1x1

http://jp1.php.net/manual/ja/language.oop5.php

Page 23: いまどきのPHP

(c) 2013 Masashi Shinbara @shin1x1

オブジェクト指向の活用

Page 24: いまどきのPHP

フレームワーク

(c) 2013 Masashi Shinbara @shin1x1

• フレームワークの利用• オブジェクト指向• MVC• デファクトスタンダードは無い

Page 25: いまどきのPHP

フレームワーク

(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

Page 26: いまどきのPHP

Symfony

(c) 2013 Masashi Shinbara @shin1x1

• コンポーネントで構成• コンポーネントは独立して利用可• 他のフレームワークやライブラリで• 部品としての品質も高い• Zend Frameworkも

Page 27: いまどきのPHP

PHPUnit

(c) 2013 Masashi Shinbara @shin1x1

http://phpunit.de/manual/3.7/ja/

Page 28: いまどきのPHP

PHPUnit

(c) 2013 Masashi Shinbara @shin1x1

• xUnit の PHP 実装• テストダブルやSelenium連携など• フレームワークやライブラリのテスト

Page 29: いまどきのPHP

PhpStorm

(c) 2013 Masashi Shinbara @shin1x1

http://www.jetbrains.com/phpstorm/

Page 30: いまどきのPHP

PhpStorm

(c) 2013 Masashi Shinbara @shin1x1

• JetBRAINS社のIDE• 補完機能が強力• オブジェクト指向に則れば恩恵大• いま注目のIDE

Page 31: いまどきのPHP

(c) 2013 Masashi Shinbara @shin1x1

最近のトピック

Page 32: いまどきのPHP

PHP 5.5 リリース

(c) 2013 Masashi Shinbara @shin1x1

• 2013/06 リリース• ジェネレータ• OPcache• パスワードハッシュ関数

Page 33: いまどきのPHP

PHP 5.3 が EOL

(c) 2013 Masashi Shinbara @shin1x1

• 5.3.27 が最後のリリース• セキュリティフィックスのみ• 2014/06 頃に終了• 今後は、5.4 or 5.5 を利用

Page 34: いまどきのPHP

BEAR.Sunday

(c) 2013 Masashi Shinbara @shin1x1

https://code.google.com/p/bearsunday/

Page 35: いまどきのPHP

BEAR.Sunday

(c) 2013 Masashi Shinbara @shin1x1

• @koriym さんが開発• リソース指向のフレームワーク• phpnw で講演• Litinum 作者も注目

Page 36: いまどきのPHP

Ginq

(c) 2013 Masashi Shinbara @shin1x1

https://github.com/akanehara/ginq

Page 37: いまどきのPHP

Ginq

(c) 2013 Masashi Shinbara @shin1x1

• @a.kanehara さんが開発• LINQ to Object を PHP に• メソッドチェインで配列操作• 遅延実行

Page 38: いまどきのPHP

注目のキーワード

(c) 2013 Masashi Shinbara @shin1x1

• PhpStorm• Composer• PSR• Vagrant

Page 39: いまどきのPHP

(c) 2013 Masashi Shinbara @shin1x1

まとめ

Page 40: いまどきのPHP

まとめ

(c) 2013 Masashi Shinbara @shin1x1

• PHPでオブジェクト指向開発• PHPらしく貪欲に成長• どう使うかはあなた次第

Page 41: いまどきのPHP

@shin1x1

(c) 2013 Masashi Shinbara @shin1x1