
自分用の備忘録
タイトルのエラーについて、日本語の解決サイトがなかったので、
備忘録として書いておきます。
Smartyの最新をインストールして
MySmarty.phpを作成して、実行したときにエラーが発生。
Smartyをいちからインストール
ここからSmartyの安定版(ver 3.2.19)をDLして、解凍するとこうなる。
サーバーのhtmlフォルダの中に、demoの中身全部と、同列にlibsをULする。
libsはSmartyというフォルダ名に変更する。
Smartyクラスの継承クラス作成
<?php
/**-----------------------------------------------
Smarty class を継承した MySmarty class
-----------------------------------------------**/
if (!class_exists("MySmarty")) {
require_once("/hoge/html/Smarty/Smarty.class.php"); // Smartyライブラリファイルを読み込む
class MySmarty extends Smarty {
var $config_dir;
var $template_dir;
var $compile_dir;
var $cache_dir;
var $plugin_dir;
function MySmarty() {
MySmarty::__construct();
}
function __destruct() {
$this->config_dir = NULL;
$this->template_dir = NULL;
$this->compile_dir = NULL;
$this->cache_dir = NULL;
$this->plugin_dir = NULL;
}
function __construct() {
require_once('/hoge/html/configs/Define.php'); // 定数のファイルを読み込む
$dir = str_replace(PUB_DIR,"",getcwd()); // 現在のディレクトリを取得
$this->config_dir = ROOT_DIR . "/configs";
$this->template_dir = ROOT_DIR . "/templates" . $dir . "/";
$this->compile_dir = ROOT_DIR . "/templates_c" . $dir . "/";
if (!is_dir($this->compile_dir)) {
mkdir($this->compile_dir, 0755 );
}
$this->cache_dir = ROOT_DIR . "/cache" . $dir . "/";
if (!is_dir($this->cache_dir)) {
mkdir($this->cache_dir, 0755 );
}
// insertなどのpluginで利用したいファイルを入れるディレクトリ
$this->plugin_dir = ROOT_DIR . "/plugin/";
$this->caching = 0;
$this->cache_lifetime = 0;
// デバッグ設定
if ($_SERVER['REMOTE_ADDR'] == MyIP) {
ini_set("display_errors", 1);
}
}
}
}
?>
ハイライトしているDefine.phpで下記のCONST変数にこのサイトのルートディレクトリの絶対パスを格納。
- ROOT_DIR
- PUB_DIR
エラー発生
これでhtml配下に入れるPHPプログラムでは、一番最初に、
require_once("class/MySmarty.php"); $mySmarty = new MySmarty();
と書けばOKなはず。SmartyのDEMOコードindex.phpにアクセスしてみると・・・
smarty – Fatal Error: Call to a member function createTemplate() on a non-object
オブジェクトがありませんとのこと。継承がきちんとできていない様子ですが
ぐぐった結果。
function __construct() {
parent::__construct();
をMySmartyクラスのfunction __construct() の直下に挿入。
これでエラーは消えました。
親のコンストラクタを正確に呼びなさいと。
あとやったこと
template_Cのフォルダと、chacheのフォルダのパーミッションを775→777に変更。
これもSmartyで書き込めないエラーがでたので、慌てて対応。
これでいちからSmartyインストールは完了。
SmartyのDEMOコードindex.phpが動きました。