「Model」「View」「Controller」の3つで処理の役割分担をするというのはなんとなくわかる。



なんとなくなのでハッキリとはわからない。
分け方に正解があるのかもわからない。



「Model」にはそのサイトのデータ処理を関数分けして羅列


「View」はどのテンプレートを使い置換処理があれば施して表示させる処理に特化させるのでほとんど改変は要らないかと。。


「Controller」は受け取ったフォームデータに必要な処理があれば施して「Model」に書いた関数を呼び出すことで実行を制御して最後に「View」に処理を任せる。



「Model」がいわゆるビジネスロジックと言われるメイン処理ということになりそうなのでコード量が多くなりそうな感じ



とりあえず書いてみました。
参考にしたのはZendのコーディング指針のページ


クラス化しないで一つのファイルに単純に関数分けしただけなので、今後これをクラス化して洗練させていきたいと思います。



■default.tpl

<html>
<body>

<h3>私の名前は%replace1%です。</h3>

</body>
</html>



■mvc.php

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);


$gl= array();



// ビジネスロジック
function model($func)
{
    global $gl;
    $gl['template']= 'default.tpl';
    $gl['errtpl']= 'error.tpl';


    function func1()
    {
        global $gl;
        $gl['replace1']= "太郎";
    }


    return $func();
}
function view()
{
    global $gl;
    $tpl= empty($gl['errmsg']) ? $gl['template'] : $gl['errtpl'];
    $html= file_get_contents($tpl);


    echo preg_replace('/%(\w+)%/e', 'isset($gl["$1"]) ? $gl["$1"] : ""', $html);
    exit;
}
function controller($func)
{
    view(model($func));
}



controller("func1");


?>





昨日作ったPHPの画像作成コードを利用して、パラメータをいろいろ変えて作った画像を動画にしてみました。


点が集まって線を形成し、線が集まって立体を形成し、これらが波打つように動きます。


動画作るの不慣れなんですが、ちょいと背伸びして最後の部分は渦巻き+収縮させてからのモーフィングに挑戦してみました。




[動画]




[使用したソフト]


AVI Maker ver1.73

WinMorph 3.01

JTrim 1.53c


今回は立体的な曲線、つまり3次元曲線を描いてみます。



■ソースコード
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);


// 600x400キャンバスを作成して内部イメージ取得
$img= imagecreatetruecolor(600, 400);



// 色を作成
$black= imagecolorallocate($img, 0, 0, 0); // 枠
$white= imagecolorallocate($img, 255, 255, 255); // 背景
// 文字用のランダムな色を100種類生成
foreach (range(1, 100) as $i){
    $color[]= imagecolorallocate($img,
    rand(0, 255), rand(0, 255), rand(0, 255));
}



// 背景色を白に
imagefill($img, 0, 0, $white);



// いろんな色のドットで3次元曲線を描く
foreach (range(0, 639) as $i){
    $min[]= 399; $max[]= 0;
}
$rad= deg2rad(1);
for ($z=200; $z>=-200; $z-=3){


    for ($x=-200; $x<=200; $x++){


        $y= 35 * (cos(sqrt($x*$x+$z*$z)*$rad) + cos(4*sqrt($x*$x+$z*$z)*$rad));
        $px= 300 + $x * cos(-30*$rad) + $z * sin(-30 * $rad);
        $py= 200 - ($y * cos(30* $rad) - (-$x * sin(-30 * $rad) + $z * cos(-30 * $rad)) * sin(30 * $rad));


        if ($py < $min[$px]){
            $min[$px]= $py;
            imagesetpixel($img, $px, $py, $color[rand(0, 99)]);
        }
        if ($py > $max[$px]){
            $max[$px]= $py;
            imagesetpixel($img, $px, $py, $color[rand(0, 99)]);
        }
    }
}



// 黒枠で囲む
imagerectangle($img, 0, 0, 599, 399, $black);



// 画像をブラウザに出力
header("Content-type: image/png");
imagepng($img);



// メモリを解放
imagedestroy($img);



?>


[表示結果]
そろそろホンキ出す-PHP GD


っぽいですね。

なんかコンピュータやってるって感じ出てきました。