PHPプログラマの技量を知りたい時
2005年だけど、こんな記事があったので書いてみた。
ちなみに私がPHPプログラマの技量を知りたい時には、ファイル数・構成は無制限で以下のような仕様のスクリプトを書いてもらいます。
* 入力ページ、確認ページ、結果表示ページから成る
* 入力ページでは郵便番号(7桁)の入力欄と送信ボタンを表示
* 入力ページで送信ボタンを押した際、郵便番号欄の値を半角に変換してハイフンを除去
* 郵便番号欄の値が「空文字列」または「7文字の半角数字以外」の場合は「郵便番号欄の値を保持」したまま入力ページを再表示
* 入力ページを再表示する再、郵便番号欄の下に入力値に応じて「入力されていません」または「入力内容が正しくありません」と表示
* 確認ページでは郵便番号、戻るボタン、次へボタンを表示
* 確認ページで戻るボタンが押された場合は「郵便番号欄の値を保持したまま」入力ページを再表示
* 確認ページで次へボタンが押された場合は結果表示ページに「入力された郵便番号はXXX-XXXXです」と表示
* ページデザインは後で差し替えるのでスクリプトとは別ファイルにしておく
* セッションは使わない
一見シンプルな仕様ですが、OOPが身に付いていない人を弾く程度には機能します。実際のところPHPプログラマとして報酬をもらうのであれば、この程度のスクリプトが設計・実装を合わせて一時間以内には書けないとマズいでしょうね(フレームワーク使えば20分くらい?)。
- 何も調べない・一から書く、の制限で書いてみたら、2時間掛かった
- もっとまともな正規表現が絶対あるはずだ・・・なんだよ^[0-9][0-9][0-9]\-?[0-9][0-9][0-9][0-9]$って
- ^[0-9]{3}\-?[0-9]{4}$ こうか・・(調べた)
- プロは凄い・・って、プロにならんといかんのだが、困ったもんだ
- もっとまともな正規表現が絶対あるはずだ・・・なんだよ^[0-9][0-9][0-9]\-?[0-9][0-9][0-9][0-9]$って
- オブジェクト指向になれてない辺りも、何とも言えない
- 大仰になる癖はなんとかならんのか
- 自分で反省するのは限界があるので、暇な方は適当に貶して頂きたい
- と思ったら半角に変換してない・・なおさら駄目っぽい
- before:$this->formString = htmlspecialchars($string);
- after : $this->formString = mb_convert_kana(htmlspecialchars($string), 'n', 'utf-8');
- 胡散臭い名前の関数だ
デバッグだけした、書いた当時のコード
./Zipcode.php
-
<?php
-
require_once('./lib/Class.php');
-
require_once('./Config.php');
-
-
$o_select = new Select_Output($_POST['input_flg']);
-
$o_select->set_form_string($_POST['zipcode']);;
-
$o_select->output();
-
?>
./Config.php
-
<?php
-
?>
./lib/Class.php
-
<?php
-
//テンプレート処理用クラス
-
class My_Template
-
{
-
private $keyword = 'mytmp:'; //置換用文字列の目印
-
private $a_replaceWords; //置換用文字列
-
private $a_template; //テンプレートファイル
-
function set_template($f_file)
-
{
-
$this->a_template = file($f_file);
-
}
-
function set_replace_word($word, $value)
-
{
-
$this->a_replaceWords[$word] = $value;
-
}
-
function output_replaced()
-
{
-
foreach($this->a_template as $key => $value) {
-
foreach($this->a_replaceWords as $k => $v) {
-
}
-
$this->a_template[$key] = $value;
-
}
-
}
-
}
-
foreach($this->a_template as $value) {
-
}
-
}
-
}
-
-
//入力確認
-
class Input
-
{
-
private $inputString;
-
private $inputStringFalse;
-
-
function set_string($string)
-
{
-
if($this->check_format($string)) {
-
$this->inputString = $string;
-
$result = TRUE;
-
} else {
-
$this->inputStringFalse = $string;
-
$result = FALSE;
-
}
-
return $result;
-
}
-
-
function get_string()
-
{
-
return $this->inputString;
-
}
-
function get_string_false()
-
{
-
return $this->inputStringFalse;
-
}
-
function check_format($string)//入力値の確認
-
{
-
$result = TRUE;
-
} else {
-
$result = FALSE;
-
}
-
return $result;
-
}
-
}
-
-
//POSTされた内容に応じて画面表示 データを自前で持たせてしまって失敗
-
class Select_Output
-
{
-
private $formString;
-
private $mode;
-
private $inputFlg;
-
-
function __construct($flg)
-
{
-
$this->inputFlg = $flg;
-
}
-
function set_form_string($string)
-
{
-
$this->formString = htmlspecialchars($string);
-
}
-
function output()
-
{
-
$o_template = new My_Template();
-
$o_input = new Input();
-
-
switch($this->inputFlg) {
-
case NULL:
-
$this->mode = 0;
-
case '0': //入力画面からPOSTされた場合
-
if($o_input->set_string($this->formString) ) {
-
$this->mode = 1;
-
} else {
-
$this->mode = 999;
-
}
-
break;
-
case '1': //確認画面から戻った場合
-
$this->mode = 0;
-
break;
-
case '2': //確認画面から進んだ場合
-
$this->mode = 2;
-
break;
-
}
-
switch($this->mode) {
-
case '0':
-
$o_template->set_template(TEMPLATE_DIR . 'Input.tpl');
-
$o_template->set_replace_word('code', $this->formString);
-
$o_template->set_replace_word('message', '郵便番号を入力してください');
-
$o_template->output_replaced();
-
break;
-
case '1':
-
$o_template->set_template(TEMPLATE_DIR . 'Confirm.tpl');
-
$o_template->set_replace_word('code', $o_input->get_string());
-
$o_template->output_replaced();
-
break;
-
case '2':
-
$o_template->set_template(TEMPLATE_DIR . 'Result.tpl');
-
} else {
-
$code = $this->formString;
-
}
-
$o_template->set_replace_word('code', $code);
-
$o_template->output_replaced();
-
break;
-
default:
-
$o_template->set_template(TEMPLATE_DIR . 'Input.tpl');
-
if($this->formString == NULL) {
-
$message = '入力されていません';
-
} else {
-
$message = '入力内容が正しくありません';
-
}
-
$o_template->set_replace_word('code', $this->formString);
-
$o_template->set_replace_word('message', $message);
-
$o_template->output_replaced();
-
-
}
-
}
-
}
-
?>
./template/Input.tpl
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-
<title>郵便番号入力</title>
-
</head>
-
<body>
-
<form method="POST" action="Zipcode.php">
-
<input type="text" name="zipcode" value="{mytmp:code}">
-
<input type="hidden" name="input_flg" value="0">
-
<input type="submit" value="送信">
-
</form>
-
{mytmp:message}
-
-
</body>
-
</html>
./template/confirm.tpl
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-
<title>郵便番号入力</title>
-
</head>
-
<body>
-
<form method="POST" action="Zipcode.php">
-
入力された値は{mytmp:code}です。
-
<input type="hidden" name="zipcode" value="{mytmp:code}">
-
<input type="hidden" name="input_flg" value="1">
-
<input type="submit" value="戻る">
-
</form>
-
-
<form method="POST" action="zipcode.php">
-
<input type="hidden" name="zipcode" value="{mytmp:code}">
-
<input type="hidden" name="input_flg" value="2">
-
<input type="submit" value="次へ">
-
</form>
-
-
</body>
-
</html>
./template/Result.tpl
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-
<title>郵便番号入力</title>
-
</head>
-
<body>
-
<form method="POST" action="Zipcode.php">
-
{mytmp:code}が保存されました。嘘。
-
<input type="hidden" name="input_flg" value="0">
-
<input type="submit" value="戻る">
-
</form>
-
-
</body>
-
</html>
No Comments