October 23, 2024
Chicago 12, Melborne City, USA
PHP

My php script can't find 'Smarty.class.php'


I’ve installed smarty via composer on my linux web server, though when I run my php test script I get:

Fatal error: Uncaught Error: Class ‘Smarty’ not found in /var/www/html/smarty_test.php:5 Stack trace: #0 {main} thrown in /var/www/html/smarty_test.php on line 5

Composer placed Smarty.class.php in /var/www/html/vendor/smarty/smarty/libs/ and my composer.json was created in /var/www/html which is also where my script is.

smarty_test.php

<?php

require_once 'vendor/autoload.php';

$smarty = new Smarty();

echo "Hello world";
echo "Smarty version: " . Smarty::SMARTY_VERSION;

?>

composer.json

{
    "require": {
        "smarty/smarty": "^5.4"
    }
}

Smarty.class.php

<?php

define('__SMARTY_DIR', __DIR__ . '/../src/');

// Global function declarations
require_once(__SMARTY_DIR . "/functions.php");

spl_autoload_register(function ($class) {
        // Class prefix
        $prefix = 'Smarty\\';

        // Does the class use the namespace prefix?
        $len = strlen($prefix);
        if (strncmp($prefix, $class, $len) !== 0) {
                // If not, move to the next registered autoloader
                return;
        }

        // Hack off the prefix part
        $relative_class = substr($class, $len);

        // Build a path to the include file
        $file = __SMARTY_DIR . str_replace('\\', '/', $relative_class) . '.php';

        // If the file exists, require it
        if (file_exists($file)) {
                require_once($file);
        }
});



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video