Document

PHPの基礎と開発手法
Based on PHP5
http://www.php.net/
Yuki Matsukura
[email protected]
9/30/2015
アウトライン
 PHPの基礎 (15分)
 説明
 実装
 PHP5の新機能 (10分)
 オブジェクト周り
 例外
 PHPを利用した開発(5分)
 開発ツール
 業界の標準
 まとめ (2分)
PHPの基礎
動作概念
 Apacheのモジュール
 一般にPHPが利用され
る
 動作軽量
 プログラム呼び出し
 コマンドラインで使用
 Perlのようなイメージ
#!/usr/local/bin/
php
<?php
…
?>
出展:http://www.uni-giessen.de/~g004/php/mysql/mysqlserver4.gif
PHPの動かし方
 <?php
で始まり
 ?>
で終わる
 拡張子
 .php
 PHPスクリプト
 .phps
 PHPソース
<html>
<h1>Say something</h1>
<?php
print “hello”;
?>
</html>
PHPの文法 変数/コメント
 変数
 型の概念は無い
 変数の宣言必要なし
 コメント
 //
 /* … */
<html>
<h1>Say something</h1>
<?php
// 代入
$message = “HELLOOOO”;
// 表示
print $message;
?>
</html>
PHPの文法 関数

利用

作る
name($param1,$param2);
function name($param1){
}
<?php
// 利用
print date('Y-m-d');
print plus(1,3);
// 作る
function plus($first, $secound){
return $first + $secound;
}
?>
PHPの文法 配列
<?php
$a1 = array('dog', 'cat', 'bird');
$a2[0] = 'dog';
$a2[1] = 'cat';
$a2[2] = 'bird';
// 表示
print '<pre>';
print_r($a1);
print_r($a2);
print '</pre>';
?>
PHPの文法 Hash
<?php
$h1 = array(
'dog' => '犬',
'cat' => '猫',
'bird' => '鳥'
);
$h2['dog'] = '犬';
$h2['cat'] = '猫';
$h2['bird'] = '鳥';
// 表示
print '<pre>';
print_r($h1);
print_r($h2);
print $h1['dog'];
print '</pre>';
?>
実装 – モデル
Formに文字を入力
(form.html)
文字列を表示
(postecho.php)
ごにょごにょ
主な機能
 ファイルアクセス
 DBアクセス
 Postgres,MySQL,Oracleなど
 Socket
 正規表現
 XML,XSL
 Session
 その他
 PDF作成
 IMAP
 LDAP
 Javaとの連携
などの便利な関数がたくさん用意されている
PHP5の新機能
簡単な歴史
 1995
 PHP/FI
 1998/6
 PHP3
 2000/5/22
 PHP4
 2004/7/13
 PHP5
オブジェクト指向
 オブジェクト指向でも書ける
 PHP4
 オブジェクト
 クラス
 PHP5(PHP4の拡張)
 アクセス制限できるようになった
新機能 - オブジェクト
 アクセス制限
 private
 protected







例外
抽象クラスとメソッド (abstract)
インタフェース (interface)
final 修飾子
Clone (__clone() )
コンストラクタ ( __construct() )
デストラクタ ( __destruct() )
Classのコード
 アクセス制限
 private
 宣言したクラスだけか
らアクセス可能
 protected
 サブクラスからもアク
セス可能
<?php
class TestClass(){
private $attribute1 = 'dog';
protected $attribute2 = 'cat';
private function getAttribute1(){
return $this->$attribute1;
}
function getAttribute2(){
return $this->$attribute2;
}
}
$class = new TestClass();
print $class->getAttribute2();
?>
例外処理
 try … throw …
catch
<?php
class TestClass(){
 Javaと同じ
private $attribute1 = 'dog';
protected $attribute2 = 'cat';
private function getAttribute1(){
return $this->$attribute1;
}
function getAttribute2(){
return $this->$attribute2;
}
}
$class = new TestClass();
print $class->getAttribute2();
?>
Classのコード
 アクセス制限
 private
 宣言したクラスだけか
らアクセス可能
 protected
 サブクラスからもアク
セス可能
<?php
class TestClass(){
private $attribute1 = 'dog';
protected $attribute2 = 'cat';
private function getAttribute1(){
return $this->$attribute1;
}
function getAttribute2(){
return $this->$attribute2;
}
}
$class = new TestClass();
print $class->getAttribute2();
?>
Classのコード
 アクセス制限
 private
 宣言したクラスだけか
らアクセス可能
 protected
 サブクラスからもアク
セス可能
<?php
class FileNotFoundException extends exception {
function FileNotFoundException($_error) {
$this->error = $_error;
}
function getException()
{
return $this->error;
}
}
class MyClass {
function open($filename)
{
if(!file_exists($filename))
throw new FileNotFoundException("file not found");
}
}
try{
$class = new MyClass();
$class->open("non_exists_file");
} catch(FileNotFoundException $e) {
die($e->getException());
}
?>
PHPを利用した開発
開発ツール
 Eclipse
 TruStudioプラグイン
 http://www.xored.com/download.php
 Apache
 PHP
 RDBMS
 Emacs?
MVC
改変:http://www.stackasterisk.jp/tech/php/mojavi01_02.jsp
フレームワーク
 mojavi
 http://www.mojavi.org/
 Phrame
 http://phrame.sourceforge.net/
 WaWaWa
 http://wawawa.jp/
優秀なクラスライブラリ達
 PEAR
 PHPにデフォルトで付属
 多種多様なライブラリ
 http://pear.php.net/
 ADOdb
 DBを抽象化し,便利なメソッドを用意
 PEARのDBより高速
 http://adodb.sourceforge.net/
 SMARTY
 Viewを分けるためのテンプレートエンジン
 http://smarty.php.net/
まとめ
まとめ
 既存のライブラリを利用する




ライブラリ全般:PEAR
DBMS抽象化:ADOdb
HTMLテンプレート:SMARTY
フレームワーク
 MVCを意識したコーディング
 MVCが入り交じったコードを書くな!
 フレームワークを利用する.
参考文献
 PHP Official Site
 http://www.php.net/
 Einführung in PHP und MySQL
 http://www.unigiessen.de/~g004/php/mysql/mysqlserv
er4.gif
 PHP5徹底攻略
 http://www.net-newbie.com/support/