0

今天我想写一个简单的php脚本,但是我遇到了一些烦人的错误。我只是简单地包含 config.php 文件并尝试访问 root_path 变量但没有成功。还有 2 个其他警告仅在我包含 config.php 文件时显示。

这些文件在最新的 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');
   }
}
?>

配置.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 回答