Lithium勉強中
cakephp3を勉強。
cakephp3と呼ばれていたフレームワーク、「Lithium」を試してみた。
CAKEPHPを作っていたプロジェクトマネージャーが作っているらしい。
http://blog.candycane.jp/archives/121
興味深いのが「PHP5.3だからこそ出来た事」と言及している点。
Lithiumの動作環境として上げられるのが、
http://labs.unoh.net/2010/07/hellolithium.html
・PHP 5.3.0 or higher
・CouchDb, MongoDb, MySQL 5 or SQLite 3 for applications utilizing a database
資料を読んでいると、
アプリケーションに頻繁にnamespaceが出てくるところ
例)Controller
<?php namespace app\controllers; use app\models\test; class TestController extends \Lithium\action\Controller{ }
あとはMongoDbというドキュメント指向データベースが使用できるので
↓こんな感じで理解していけば良いみたい。
1.MongDBについて
2.PHP5.3以降で使用できるようになったnamespace
3.lithiumについて
調査
[mongoDB]-------------------------------------------
ドキュメント指向データベース
noスキーマ
データ定義自体が各ドキュメントに入っている
RDBMS>
行と列にデータがあてはめられる
ドキュメント指向
ID:1
名前:ああ
ID:2
名前:いい
ID:3
名前:うう
スコア:100
AGPL(GPLではないので、WEB公開する場合でもソースを公開しなければならぬ)
データをBSONで保存
http://handsout.jp/slide/2316
トランザクションは存在しない。
[php5.3からの新機能]-------------------------------------------
★_callStatic() マジックメソッド
呼び出されたクラスメソッドが存在しない場合、
変わりに呼び出される
<?php class Hoge{ public static function test(){ echo 'test1'; ] public static function __callStatic($name,$args){ echo $name; } } Hoge::test(); // test1 Hoge::test2(); //test2
★クラスメソッドの動的呼びだし
動的に変数、関数を変更する仕組み
<?php class Test{ public static $name = 'AAAA'; public static function go(){ echo 'go!go!'; } } $class = 'Test'; $action = 'go'; echo $class::$name; $class::go(); $class::$action();
★遅延静的バインディング
<?php class Test{ protected static $type = 'AAA'; public static function get(){ return self::$type; } } class Test2 extends Test{ protected static $type = 'BBB'; } echo Test2::get();//AAA
★無名関数
実行時に関数を生成する
名前を付けずに関数オブジェクトを生成する
<?php $func = function($a,$b){ return $a+$b; ] echo $func(1,2); //コールバックに便利! calss Test{ public $a; public function __constract(){ $this->a = mt_rand(); } ] $objs = array(); for(4i = 0;$i<1000;++$i){ $objs[] = new Test; } usort($objs,function($a,$b){ return $a->a - $b->a; });
無名関数に独自の変数評価環境を持たせたもの
1つ以上のバインド変数を持つ
<?php $string = 'hoge'; $func = function() use($string){ echo $string; } $func(); $string = 'fuga'; $func();
★名前空間
namespace
PHP5.0に入りかけたが中止。
<?php namespace SciFi; class Galactica{ public function __constuct(){ echo 'Galactica constuctor called'; } static public functionshow(){ } } $obj = new |SciFil|Galactica; |SciFil|Galactica::show();
名前空間の区切り方
\バックスラッシュ
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
名前空間とは。。
http://d.hatena.ne.jp/red_snow/20100825/1282711785
を参考。
広義の名前空間とは項目を「カプセル化」するもののこと。
多くの場面で見られる抽象概念。
問題1 例えば,foo.txtというファイルは/home/gregと/home/otherの両方に存在することができるが
同じディレクトリに存在させることはできない。
問題2 /home/greg外からfoo.txtにアクセスするにはディレクトリ名をファイル名の前につけて、
/home/greg/foo.txtとしなければならない。
名前空間は2つの問題を解決する為に作られる。
例えば、ライブラリやアプリケーション作者がクラスや関数などの再利用可能なコード
部品をつくるときに、これらの問題がぶちあたる。
試すと
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
<?php namespace foo; class MyClass { public function MyMethod(){ return __METHOD__; } } namespace bar; class MyClass { public function MyMethod(){ return __METHOD__; } } $obj1 = new \foo\Myclass; $obj2 = new \bar\Myclass; $obj3 = new MyClass; var_dump($obj1->MyMethod()); //=>string(21) "foo\MyClass::MyMethod" var_dump($obj2->MyMethod()); //=>string(21) "bar\MyClass::MyMethod" var_dump($obj3->MyMethod()); //=>string(21) "bar\MyClass::MyMethod"
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
本来はnamespaceを1ファイルに書くのは好ましくない。
★例1
<?php namespace foo{ class MyClass { public function MyMethod(){ return __METHOD__; } } } namespace{ $class = 'foo\MyClass'; $obj = new $class; var_dump(get_class($obj));//+>string(12)"foo/MyClass" }
↑グローバル名前空間も使用できる。
グローバル名前空間とは最上位の名前空間であり、暗黙的に定義される無名の名前空間。
★例2
インポート
<?php namespace hoge; require 'index.php'; use foo\MyClass; $obj = new MyClass(); var_dump($obj->MyClass());//=>string(21) "foo\MyClass::MyMethod"
★例3
オーバーライド
namespace hoge;
function file_put_contents($filename,$data,$flags =0,$context =null)
{
$return = \file_put_contents($filename,$data,$context);
chmod($filename,0444);
return $return;
}
?>
[lithiumについて]
http://rad-dev.org/lithium/versions
0)
少なくとも
httpとphpの5.3以降が入ってる状態にする。
mysql-serverもか。
1)フォルダ移動
/var/www/html配下にlithiumのフォルダを全てつっこむ
/var/www/html/lithium/app/config・・みたいな感じ。
2)http.confの設定でドキュメントルートを
/var/www/html/lithium/app/webrootに設定する
NameVirtualHost *:80 <VirtualHost *:80> ServerName IPアドレス DocumentRoot /var/www/html/lithium/app/webroot <Directory "/var/www/html/lithium/app/webroot"> DirectoryIndex index.php AllowOverride All Order deny,allow Allow from all </Directory> </VirtualHost>
3)http://IPアドレス
にアクセスする
4)/var/www/html/lithium/app/resourcesに
書き込み権限を与える
5)/var/www/html/lithium/app/config/bootstrap.phpの
require __DIR__ . '/bootstrap/connections.php';
を有効にする。コメントアウト。元々されてるかも。
6)mysqlの接続先を記載する
vi /var/www/html/lithium/app/config/bootstrap/connections.php
/** * Uncomment this configuration to use MySQL as your default database. */ Connections::add('default', array( 'type' => 'database', 'adapter' => 'MySql', 'host' =>'1111111111', 'login' => 'aaaaaaaaaa', 'password' => 'aaaaaaaaaaaaa', 'database' => 'aaaaaaaaa' ));