0

今日は単純な php スクリプトを書きたかったのですが、厄介なエラーが発生しました。config.php ファイルをインクルードし、root_path 変数にアクセスしようとしましたが、成功しませんでした。config.php ファイルをインクルードした場合にのみ表示される 2 つの警告もあります。

ファイルは最新の xampp で実行されています。

smarty_setup.php :

<?php
require('config.php');
require($root_path . '/libs/Smarty.class.php');

class FileHosting extends Smarty {

   function __construct()
   {
        parent::__construct();

        $this->setTemplateDir($root_path . '/templates/');
        $this->setCompileDir($root_path . '/templates_c/');
        $this->setConfigDir($root_path . '/configs/');
        $this->setCacheDir($root_path . '/cache/');
        $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
        $this->assign('app_name', 'File Hosting');
   }
}
?>

config.php :

<?php
    $root_path = 'D:/xampp/htdocs/example';
    $db_user = 'xxx';
    $db_password = 'xxx';
    $db_name = 'xxx';
    $db_host = 'xxx';
    $facebook_appID = 'xxx';
    $facebook_secret = 'xxx';
?>

エラー:

Deprecated: Assigning the return value of new by reference is deprecated in D:\xampp\php\PEAR\Config.php on line 80

Deprecated: Assigning the return value of new by reference is deprecated in D:\xampp\php\PEAR\Config.php on line 166

Notice: Undefined variable: root_path in D:\xampp\htdocs\example\includes\smarty_setup.php on line 3

Notice: Undefined variable: root_path in D:\xampp\htdocs\example\includes\smarty_setup.php on line 11

Notice: Undefined variable: root_path in D:\xampp\htdocs\example\includes\smarty_setup.php on line 12

Notice: Undefined variable: root_path in D:\xampp\htdocs\example\includes\smarty_setup.php on line 13

Notice: Undefined variable: root_path in D:\xampp\htdocs\example\includes\smarty_setup.php on line 14

私を助けてくれてありがとう。

4

2 に答える 2

1

クラス内$root_pathでは、グローバルスコープにあるにアクセスしています。それをコンストラクターに渡します。

class FileHosting extends Smarty {

   // Pass $root_path as a param to the constructor
   function __construct($root_path)
   {
        parent::__construct();

        $this->setTemplateDir($root_path . '/templates/');
        $this->setCompileDir($root_path . '/templates_c/');
        $this->setConfigDir($root_path . '/configs/');
        $this->setCacheDir($root_path . '/cache/');
        $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
        $this->assign('app_name', 'File Hosting');
   }
}

// Instantiate as
$smarty = new FileHosting($root_path);

これらのエラーの最初のものは、config.php適切に含まれていなかったことを示しているため、不可解です。

Notice: Undefined variable: root_path in D:\xampp\htdocs\example\includes\smarty_setup.php on line 3

それらが実際に唯一の内容である場合config.php(たとえば、関数内でこれらの変数を設定していない場合)、その最初のroot_path通知を受け取るべきではありません。

アップデート

config.php相対パスに含めることに失敗した場合は、次のことを確認してください。

  1. config.phpインクルードしようとしているファイルと同じディレクトリにあります
  2. PHPinclude_pathをチェックして、現在のディレクトリが含まれていることを確認し.ます。

echo get_include_path();
于 2012-05-09T13:41:24.597 に答える
0

$root_pathを変数ではなく定数にしてみてください。

define('ROOT_PATH', 'D:/xampp/htdocs/example');

そしてそれを次のように使用します:

$this->setTemplateDir(ROOT_PATH . '/templates/');

于 2012-05-09T13:42:04.137 に答える