简体   繁体   English

找不到Smarty_Internal_TemplateCompilerBase

[英]Smarty_Internal_TemplateCompilerBase not found

composer.json: composer.json:

{
  "require": {
    "smarty/smarty": "v3.1.17"
  }
}

index.php: index.php:

define('SMARTY_SPL_AUTOLOAD', 1); // now smarty should use its own autoloader

require_once __DIR__ . "/vendor/autoload.php";

function my_classes_loader($class) {
  $path = "{$class}.class.php";

  if (file_exists($path)) {
    include_once $path;
    return true;
  }

  return false;
}

spl_autoload_register('my_classes_loader');

$smarty = new Smarty();
$smarty->setCompileDir("templates_c");

$smarty->display('main.html');

exit();

If I open it in browser I get 如果我在浏览器中打开它

Fatal error: Class 'Smarty_Internal_TemplateCompilerBase' not found in //smarty-example/vendor/smarty/smarty/distribution/libs/sysplugins/smarty_internal_smartytemplatecompiler.php on line XX 致命错误:在第XX行的//smarty-example/vendor/smarty/smarty/distribution/libs/sysplugins/smarty_internal_smartytemplatecompiler.php中找不到类'Smarty_Internal_TemplateCompilerBase'

The file is there. 文件在那里。 And it has content. 它有内容。 And it is accessible / readable for PHP etc. 它是存取/可读的PHP等等。

What am I doing wrong? 我究竟做错了什么? What is missing? 什么东西少了?

It is a good idea to only have one point deciding about autoloading, and this should be Composer alone. 一个好主意是决定自动加载,这应该是Composer一个人。

Try to put your own autoload function away, use a autoload declaration in your composer.json instead. 尝试放弃自己的自动加载功能,而在composer.json中使用自动加载声明。 Unfortunately you are not using either PSR-0 or PSR-4 naming standard, but Composer allows you to use "classmap" in this case. 不幸的是,您没有使用PSR-0或PSR-4命名标准,但是在这种情况下,Composer允许您使用“ classmap”。 Consider moving all your file names to comply with PSR-0. 考虑移动所有文件名以符合PSR-0。

And the smarty autoloading should already be done by requiring it via Composer. 聪明的自动加载应该已经通过Composer要求完成。 No need to set that constant. 无需设置该常数。

Last but not least I think your autoload function should not return anything. 最后但并非最不重要的一点是,我认为您的自动加载功能不应返回任何内容。 Especially it should not return false if it cannot find the file it supposes to contain the class, because depending on how the autoload functions are ordered on the stack, your function might get called first for ALL classes, including all Smarty ones. 特别是,如果找不到找不到的包含类的文件,则它不应返回false,因为根据自动加载函数在堆栈上的排序方式,您的函数可能首先被所有类(包括所有Smarty类)调用。 If you return false in these cases, you destroy the working autoload stack by not allowing later functions to load that class. 如果在这些情况下返回false,则通过不允许更高版本的函数加载该类来破坏工作中的自动加载堆栈。

So all in all it is best to use Composer for all autoloading. 因此总而言之,最好使用Composer进行所有自动加载。 The developers did everything to provide the best performing autoload function - your own function probably can only be as fast as theirs, but will probably be slower. 开发人员竭尽所能提供最佳性能的自动加载功能-您自己的功能可能只能与他们的功能一样快,但可能会更慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM