Ayas置き場
RSS Feeds
1112月

PHPプログラマの技量を知りたい時

 2005年だけど、こんな記事があったので書いてみた。

ちなみに私がPHPプログラマの技量を知りたい時には、ファイル数・構成は無制限で以下のような仕様のスクリプトを書いてもらいます。

* 入力ページ、確認ページ、結果表示ページから成る

* 入力ページでは郵便番号(7桁)の入力欄と送信ボタンを表示

* 入力ページで送信ボタンを押した際、郵便番号欄の値を半角に変換してハイフンを除去

* 郵便番号欄の値が「空文字列」または「7文字の半角数字以外」の場合は「郵便番号欄の値を保持」したまま入力ページを再表示

* 入力ページを再表示する再、郵便番号欄の下に入力値に応じて「入力されていません」または「入力内容が正しくありません」と表示

* 確認ページでは郵便番号、戻るボタン、次へボタンを表示

* 確認ページで戻るボタンが押された場合は「郵便番号欄の値を保持したまま」入力ページを再表示

* 確認ページで次へボタンが押された場合は結果表示ページに「入力された郵便番号はXXX-XXXXです」と表示

* ページデザインは後で差し替えるのでスクリプトとは別ファイルにしておく

* セッションは使わない

Zend PHP Certification - Sooey

一見シンプルな仕様ですが、OOPが身に付いていない人を弾く程度には機能します。実際のところPHPプログラマとして報酬をもらうのであれば、この程度のスクリプトが設計・実装を合わせて一時間以内には書けないとマズいでしょうね(フレームワーク使えば20分くらい?)。

Zend PHP Certification - Sooey

  • 何も調べない・一から書く、の制限で書いてみたら、2時間掛かった
    • もっとまともな正規表現が絶対あるはずだ・・・なんだよ^[0-9][0-9][0-9]\-?[0-9][0-9][0-9][0-9]$って
      • ^[0-9]{3}\-?[0-9]{4}$ こうか・・(調べた)
    • プロは凄い・・って、プロにならんといかんのだが、困ったもんだ
  • オブジェクト指向になれてない辺りも、何とも言えない
  • 大仰になる癖はなんとかならんのか
  • 自分で反省するのは限界があるので、暇な方は適当に貶して頂きたい
  • と思ったら半角に変換してない・・なおさら駄目っぽい
    • before:$this->formString = htmlspecialchars($string);
    • after : $this->formString = mb_convert_kana(htmlspecialchars($string), 'n', 'utf-8');
    • 胡散臭い名前の関数だ

デバッグだけした、書いた当時のコード

./Zipcode.php

PHP:
  1. <?php
  2. require_once('./lib/Class.php');
  3. require_once('./Config.php');
  4.  
  5. $o_select = new Select_Output($_POST['input_flg']);
  6. $o_select->set_form_string($_POST['zipcode']);;
  7. $o_select->output();
  8. ?>

./Config.php

PHP:
  1. <?php
  2. define('TEMPLATE_DIR', './template/');
  3. ?>

./lib/Class.php

PHP:
  1. <?php
  2. //テンプレート処理用クラス
  3. class My_Template
  4. {
  5.     private $keyword = 'mytmp:';    //置換用文字列の目印
  6.     private $a_replaceWords;        //置換用文字列
  7.     private $a_template;            //テンプレートファイル
  8.     function set_template($f_file)
  9.     {
  10.         $this->a_template = file($f_file);
  11.     }
  12.     function set_replace_word($word, $value)
  13.     {
  14.         $this->a_replaceWords[$word] = $value;
  15.     }
  16.     function output_replaced()
  17.     {
  18.         if(isset($this->a_replaceWords) ){
  19.             foreach($this->a_template as $key => $value) {
  20.                 if(ereg('{' . $this->keyword . '.*}', $value) ) {
  21.                     foreach($this->a_replaceWords as $k => $v) {
  22.                         $value = str_replace('{' . $this->keyword . $k . '}', $v, $value);
  23.                     }
  24.                 $this->a_template[$key] = $value;
  25.                 }
  26.             }
  27.         }
  28.         foreach($this->a_template as $value) {
  29.             print($value);
  30.         }
  31.     }
  32. }
  33.  
  34. //入力確認
  35. class Input
  36. {
  37.     private $inputString;
  38.     private $inputStringFalse;
  39.  
  40.     function set_string($string)
  41.     {
  42.         if($this->check_format($string)) {
  43.             $this->inputString = $string;
  44.             $result = TRUE;
  45.         } else {
  46.             $this->inputStringFalse = $string;
  47.             $result = FALSE;
  48.         }
  49.         return $result;
  50.     }
  51.  
  52.     function get_string()
  53.     {
  54.         return $this->inputString;
  55.     }
  56.     function get_string_false()
  57.     {
  58.         return $this->inputStringFalse;
  59.     }
  60.     function check_format($string)//入力値の確認
  61.     {
  62.         if(ereg('^[0-9][0-9][0-9]\-?[0-9][0-9][0-9][0-9]$', $string) ) {
  63.             $string = str_replace('-', '', $string);
  64.             $result = TRUE;
  65.         } else {
  66.             $result = FALSE;
  67.         }
  68.         return $result;
  69.     }
  70. }
  71.  
  72. //POSTされた内容に応じて画面表示 データを自前で持たせてしまって失敗
  73. class Select_Output
  74. {
  75.     private $formString;
  76.     private $mode;
  77.     private $inputFlg;
  78.  
  79.     function __construct($flg)
  80.     {
  81.         $this->inputFlg = $flg;
  82.     }
  83.     function set_form_string($string)
  84.     {
  85.         $this->formString = htmlspecialchars($string);
  86.     }
  87.     function output()
  88.     {
  89.         $o_template = new My_Template();
  90.         $o_input = new Input();
  91.  
  92.         switch($this->inputFlg) {
  93.             case NULL:
  94.                 $this->mode = 0;
  95.             case '0':   //入力画面からPOSTされた場合
  96.                 if($o_input->set_string($this->formString) ) {
  97.                     $this->mode = 1;
  98.                 } else {
  99.                     $this->mode = 999;
  100.                 }
  101.                 break;
  102.             case '1':   //確認画面から戻った場合
  103.                 $this->mode = 0;
  104.                 break;
  105.             case '2':   //確認画面から進んだ場合
  106.                 $this->mode = 2;
  107.                 break;
  108.         }
  109.         switch($this->mode) {
  110.             case '0':
  111.                 $o_template->set_template(TEMPLATE_DIR . 'Input.tpl');
  112.                 $o_template->set_replace_word('code', $this->formString);
  113.                 $o_template->set_replace_word('message', '郵便番号を入力してください');
  114.                 $o_template->output_replaced();
  115.                 break;
  116.             case '1':
  117.                 $o_template->set_template(TEMPLATE_DIR . 'Confirm.tpl');
  118.                 $o_template->set_replace_word('code', $o_input->get_string());
  119.                 $o_template->output_replaced();
  120.                 break;
  121.             case '2':
  122.                 $o_template->set_template(TEMPLATE_DIR . 'Result.tpl');
  123.                 if(!ereg('\-', $this->formString) ) {
  124.                     $code = substr($this->formString, 0,3) . '-' . substr($this->formString, 3, 6);
  125.                 } else {
  126.                     $code = $this->formString;
  127.                 }
  128.                 $o_template->set_replace_word('code', $code);
  129.                 $o_template->output_replaced();
  130.                 break;
  131.             default:
  132.                 $o_template->set_template(TEMPLATE_DIR . 'Input.tpl');
  133.                 if($this->formString == NULL) {
  134.                     $message = '入力されていません';
  135.                 } else {
  136.                     $message = '入力内容が正しくありません';
  137.                 }
  138.                 $o_template->set_replace_word('code', $this->formString);
  139.                 $o_template->set_replace_word('message', $message);
  140.                 $o_template->output_replaced();
  141.  
  142.         }
  143.     }
  144. }
  145. ?>

./template/Input.tpl

PHP:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>郵便番号入力</title>
  6. </head>
  7. <body>
  8. <form method="POST" action="Zipcode.php">
  9. <input type="text" name="zipcode" value="{mytmp:code}">
  10. <input type="hidden" name="input_flg" value="0">
  11. <input type="submit" value="送信">
  12. </form>
  13. {mytmp:message}
  14.  
  15. </body>
  16. </html>

./template/confirm.tpl

PHP:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>郵便番号入力</title>
  6. </head>
  7. <body>
  8. <form method="POST" action="Zipcode.php">
  9. 入力された値は{mytmp:code}です。
  10. <input type="hidden" name="zipcode" value="{mytmp:code}">
  11. <input type="hidden" name="input_flg" value="1">
  12. <input type="submit" value="戻る">
  13. </form>
  14.  
  15. <form method="POST" action="zipcode.php">
  16. <input type="hidden" name="zipcode" value="{mytmp:code}">
  17. <input type="hidden" name="input_flg" value="2">
  18. <input type="submit" value="次へ">
  19. </form>
  20.  
  21. </body>
  22. </html>

./template/Result.tpl

PHP:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>郵便番号入力</title>
  6. </head>
  7. <body>
  8. <form method="POST" action="Zipcode.php">
  9. {mytmp:code}が保存されました。嘘。
  10. <input type="hidden" name="input_flg" value="0">
  11. <input type="submit" value="戻る">
  12. </form>
  13.  
  14. </body>
  15. </html>

by Ayas 0 comments Category: PHP

No Comments