Smartyクラスを継承して使いやすいように拡張。(php proの拡張セットアップがなぜかうまく動かなかったので・・。

拡張セットアップ - PHPプロ!マニュアル
こちらを参考に色々やってみた。
参考ソース

vi smarty_test/setup.php

<?php
require('Smarty.class.php');

class Smarty_Test extends Smarty {
  function Smrty_Test() {
    $this->Smarty();
    $this->template_dir = '(ファイルの絶対パス)/smarty_test/templates/';
    $this->compile_dir  = '(ファイルの絶対パス)/smarty_test/templates_c/';
    $this->config_dir   = '(ファイルの絶対パス)/smarty_test/configs/';
    $this->cache_dir    = '(ファイルの絶対パス)/smarty_test/cache/';
    $this->caching = true;
    $this->assign('app_name', 'Smarty Test');
  }
}
?>

呼び出し元は
vi smarty_test/htdocs/index.php

<?php
require_once('../setup.php');
$smarty = new Smarty_Test();
$smarty->assign('name','Ryoff');
$smarty->display('index.tpl');
?>

ほぼ(ってか全て)、php proからソース持ってきただけです


これ動かすと、

Warning: Smarty error: unable to read resource: "index.tpl" in /usr/local/lib/php/Smarty/Smarty.class.php on line 1093
Fatal error: Smarty error: the $compile_dir 'templates_c' does not exist, or is not a directory. in /usr/local/lib/php/Smarty/Smarty.class.php on line 1093

こんなエラーでてうまくいかねぇ。
var_dumpしてみると、コンストラクタうまく呼び出せてない感じ。


なんでだろう、って調べたら、どうやらこれは php4 の記述方法らしい。

php5では

Andante PHP、Smartyクラスを継承して拡張したい
↑のサイト様にお世話になりました。

parent::__construct(); //親クラス初期化

こんな感じで親のコンストラクタを明示的に呼び出せるらしい。


なので書き直してみた。
vi smarty_test/setup.php

<?php
require('Smarty.class.php');

class Smarty_Test extends Smarty {
  public function __construct() {
    parent::__construct(); //親クラス初期化
    $this->template_dir = '(ファイルの絶対パス)/smarty_test/templates/';
    $this->compile_dir  = '(ファイルの絶対パス)/smarty_test/templates_c/';
    $this->config_dir   = '(ファイルの絶対パス)/smarty_test/configs/';
    $this->cache_dir    = '(ファイルの絶対パス)/smarty_test/cache/';
    $this->caching = true;
    $this->assign('app_name', 'Smarty Test');
  }
}
?>

動いた。


非常に勉強になりました。