ロード 第1章



 Cakephp2 study
    2011/6/25

   Ichikaway
自己紹介

Yasushi Ichikawa
@cakephper
http://d.hatena.ne.jp/cakephper
KANAEL


http://kanael.net
LOADING

•   ファイル探索
•   ファイルインポート(include, require)
•   インスタンス生成
アジェンダ

•   App::build()
•   App::import()
•   AutoLoading
•   LazyLoading
App::build()

•   app/bootstrap.phpに定義
•   標準以外の読み込みPATHを定義
     –   ex. 複数のプラグインフォルダを使いたい
App::build()
                                   1.3
App::build(array(
 'controllers' => array(
   '/home/user/app1/Controller',
   '/home/user/app2/Controller',
  )
))
                                   2.0
App::build(array(
 'Controller' => array(
   '/home/user/app1/Controller',
   '/home/user/app2/Controller',
  )
))
アジェンダ

•   App::build()
•   App::import()
•   AutoLoading
•   LazyLoading
App::import()

•   ファイルの読み込み(include/require)
    –   例 App::import('Controller', 'UserGroup');
•   ファイル探索
    –   1.3 : ディレクトリ再帰探索
    –   2.0 : 再帰探索しない
•   ファイルのインポート処理
    –   1.3:すぐにinclude, require
    –   2.0:クラス利用時にinclude, require
アジェンダ

•   App::build()
•   App::import()
•   AutoLoading
•   LazyLoading
Autoload
•   __autoload()
     –   未定義のクラス/インターフェイスを使用し
         ようとした時に 自動的にコールされる
     –   new Hoge()するまでrequireしない
          •   インポート負荷の軽減

     function __autoload($class_name) {
         include $class_name . '.php';
     }
     $obj = new MyClass1();
     $obj2 = new MyClass2();
Autoload
•   App::uses('EmailComponent', 'Controller/Component')
     –   クラス名と格納場所を登録するのみ
•   new EmailComponent()した時にautoloadの仕組みを
    使ってincludeされる
     –   App::load()がspl_autoload_register()から呼び出され
         る
     –   cake/Cake/bootstrap.php
         •   spl_autoload_register(array('App', 'load'));
Autoload
Autoloadについて詳しく知りたい人は
アジェンダ

•   App::build()
•   App::import()
•   AutoLoading
•   LazyLoading
LazyLoading

•   AutoLoadingはinclude/requireのタイミング
•   LazyLoadingはインスタンス生成のタイミング
LazyLoading

•    CakePHP1.3
      –    アクションで利用しないモデルオブジェクトまで生成

    class PostsController extends AppController {
          public $uses = array('Post','Event');
        function index() {
            $this->Post->recursive = 0;
            $this->set('posts', $this-
    >paginate());
        }
LazyLoading

•   CakePHP1.3
     –   cake/dispatcher.php
     –   controller::constructClasses()
     –   controller::loadModel()
     –   ClassRegistry::init()
           •   new Model()
LazyLoading

•   CakePHP2.0
     –   Controller::usesで定義してもモデルオブジェク
         トは生成されない
     –   コントローラのプロパティ変数($this->Model)
         にアクセスした時に初めてオブジェクト生成
          •   不要なオブジェクト生成を回避
          •   マジックメソッドを利用
LazyLoading

•   CakePHP2.0
     –   $this->Post->find(); //controller
     –   Controller::__get() でisset()
     –   Controller::__isset()
     –   Controller::loadModel()
     –   ClassRegistry::init()
           •   new Model()
Conclusion

•   CakePHP2.0
     –   Autoload, LazyLoadの機能によりパフォーマ
         ンスアップ
     –   App::import()のPath再帰探索がなくなったの
         で注意
ロード 第2章へ・・・



  Thank you
 http://www.facebook.com/CakeRadioGaGa

CakePHP2 Loading (Japanese)

  • 1.
    ロード 第1章 Cakephp2study 2011/6/25 Ichikaway
  • 2.
  • 3.
  • 4.
    LOADING • ファイル探索 • ファイルインポート(include, require) • インスタンス生成
  • 5.
    アジェンダ • App::build() • App::import() • AutoLoading • LazyLoading
  • 6.
    App::build() • app/bootstrap.phpに定義 • 標準以外の読み込みPATHを定義 – ex. 複数のプラグインフォルダを使いたい
  • 7.
    App::build() 1.3 App::build(array(  'controllers' => array(    '/home/user/app1/Controller',    '/home/user/app2/Controller',   ) )) 2.0 App::build(array(  'Controller' => array(    '/home/user/app1/Controller',    '/home/user/app2/Controller',   ) ))
  • 8.
    アジェンダ • App::build() • App::import() • AutoLoading • LazyLoading
  • 9.
    App::import() • ファイルの読み込み(include/require) – 例 App::import('Controller', 'UserGroup'); • ファイル探索 – 1.3 : ディレクトリ再帰探索 – 2.0 : 再帰探索しない • ファイルのインポート処理 – 1.3:すぐにinclude, require – 2.0:クラス利用時にinclude, require
  • 10.
    アジェンダ • App::build() • App::import() • AutoLoading • LazyLoading
  • 11.
    Autoload • __autoload() – 未定義のクラス/インターフェイスを使用し ようとした時に 自動的にコールされる – new Hoge()するまでrequireしない • インポート負荷の軽減 function __autoload($class_name) { include $class_name . '.php'; } $obj = new MyClass1(); $obj2 = new MyClass2();
  • 12.
    Autoload • App::uses('EmailComponent', 'Controller/Component') – クラス名と格納場所を登録するのみ • new EmailComponent()した時にautoloadの仕組みを 使ってincludeされる – App::load()がspl_autoload_register()から呼び出され る – cake/Cake/bootstrap.php • spl_autoload_register(array('App', 'load'));
  • 13.
  • 14.
    アジェンダ • App::build() • App::import() • AutoLoading • LazyLoading
  • 15.
    LazyLoading • AutoLoadingはinclude/requireのタイミング • LazyLoadingはインスタンス生成のタイミング
  • 16.
    LazyLoading • CakePHP1.3 – アクションで利用しないモデルオブジェクトまで生成 class PostsController extends AppController { public $uses = array('Post','Event'); function index() { $this->Post->recursive = 0; $this->set('posts', $this- >paginate()); }
  • 17.
    LazyLoading • CakePHP1.3 – cake/dispatcher.php – controller::constructClasses() – controller::loadModel() – ClassRegistry::init() • new Model()
  • 18.
    LazyLoading • CakePHP2.0 – Controller::usesで定義してもモデルオブジェク トは生成されない – コントローラのプロパティ変数($this->Model) にアクセスした時に初めてオブジェクト生成 • 不要なオブジェクト生成を回避 • マジックメソッドを利用
  • 19.
    LazyLoading • CakePHP2.0 – $this->Post->find(); //controller – Controller::__get() でisset() – Controller::__isset() – Controller::loadModel() – ClassRegistry::init() • new Model()
  • 20.
    Conclusion • CakePHP2.0 – Autoload, LazyLoadの機能によりパフォーマ ンスアップ – App::import()のPath再帰探索がなくなったの で注意
  • 21.
    ロード 第2章へ・・・ Thank you http://www.facebook.com/CakeRadioGaGa