内部服务器错误新对象 php

Internal server error new object php

这是我的代码。我不知道为什么创建新的对象名称站点会产生错误。我对象的文件名也与 class 名称 site.php.

相同
<?php
class site {
    var $is_container_enabled = 1; 
    function init() {
        $this->container_tpl = "common/common";
    }
    function handle_page($page) {
        $this->cache_id = $page;
        $this->smarty->caching = 0;
        switch ($page) {
            case "static" :
                $type=$_REQUEST['choice'];
                $this->default_tpl = "static/$type";
                break;

            default :
                $this->is_container_enabled = isset($_REQUEST['ce'])?$_REQUEST['ce']:1;
                $this->default_tpl = $page.'/home';
                break;
        }
    }
    function is_container_enabled(){
        return $this->is_container_enabled;
    }
    function set_container_enabled($ce){
        $this->is_container_enabled = $ce;
    }
    function get_container_tpl(){
        return $this->container_tpl;
    }
}
    

一些index.php调用对象站点

  echo "before site declaration";
    $site = new site; // cause error
    echo "after site declaration";
            
    $site->init();
    $smarty = getSmarty();
    $site->smarty= &$smarty;
    $site->cache_id= &$cache_id;

Result in the browser image

这里还有 Console error image

根据评论中提到的@Quasimodo 的克隆,您需要确保在要执行的文件中包含 class:

use site; // if you are using namespacing
include 'path/to/site'; // if you want to directly include the file
require 'path/to/site'; // same as include but will throw an E_COMPILE_ERROR if it wasn't found

echo "before site declaration";
$site = new site;
echo "after site declaration";

如果事实证明您已经包含该文件,那么请考虑注释掉 site class 中的所有方法并查看它是否仍然无法实例化。可能是您的 class.

中存在语法错误
include 'site.php';
echo "before site declaration";
$site = new site; // You can access member variables/functions with this
echo "after site declaration";

$site->init();
$smarty = getSmarty();
$site->smarty= &$smarty;
$site->cache_id= &$cache_id;

试试这个