PHP-未定义的变量和其他错误

PHP-未定义的变量和其他错误,php,undefined,deprecated,Php,Undefined,Deprecated,今天我想写一个简单的php脚本,但是我遇到了一些恼人的错误。 我只是简单地包含config.php文件并尝试访问root_path变量,但没有成功。还有另外两个警告,只有在我包含config.php文件时才会显示 这些文件正在最新的xampp上运行 smarty_setup.php: <?php require('config.php'); require($root_path . '/libs/Smarty.class.php'); class FileHosting extends

今天我想写一个简单的php脚本,但是我遇到了一些恼人的错误。 我只是简单地包含config.php文件并尝试访问root_path变量,但没有成功。还有另外两个警告,只有在我包含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');
   }
}
?>

谢谢您的帮助。

在您的类中,您正在访问位于全局范围的
$root\u 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
的唯一内容(例如,您没有在函数中设置这些变量),那么您不应该得到第一个
根路径的通知

更新 如果无法使用相对路径包含
config.php
,请确保:

  • config.php
    与您试图从中包含它的文件位于同一目录中
  • 检查您的PHP
    include_path
    ,确保它包含当前目录

  • 尝试将$root_path设置为常量而不是变量:

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

    然后像这样使用它:


    $this->setTemplateDir(根路径'/templates/')

    您的问题是什么?信息是否不清楚?信息是否清楚。我只是不明白为什么我不能访问变量/为什么它们不存在。好的,谢谢。难道没有别的办法吗?因为我需要Smarty.class.php include的路径。@user1384731奇怪的是,你应该已经为include提供了路径。只要
    config.php
    实际上是
    require()
    'd并正确解析,就会设置
    $root\u path
    并可用于包含smarty。是的,这真的很奇怪。现在使用root_path仅用于包含第二个文件,并且存在相同的错误。还尝试了包含等。仔细检查了所有内容。。但是找不到任何错误。XAMPP是新安装的,只启用了curl并为root用户设置了密码。好吧,我现在尝试使用一个绝对路径包含config.php(这有点愚蠢,因为我需要config.php中的路径),所有操作都没有任何错误。有什么想法吗?@user1384731检查我上面添加的包含路径。然后包含文件时出现问题。这一次您遇到了什么错误?这是我尝试使用常量时出现的错误:注意:在第3行的D:\xampp\htdocs\hosting\includes\smarty\u setup.php中使用未定义的常量ROOT\u PATH-假定为“ROOT\u PATH”[函数.require]:无法打开流:第3行的D:\xampp\htdocs\hosting\includes\smarty\u setup.php中没有此类文件或目录定义包含问题-测试是否包含所有必要的文件,您将解决所有问题。
    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);
    
    Notice: Undefined variable: root_path in D:\xampp\htdocs\example\includes\smarty_setup.php on line 3
    
    echo get_include_path();