内部服务器错误新对象php

时间:2017-04-30 13:26:15

标签: php object internal-server-error

这是我的代码。我不知道为什么创建新的对象名称网站会产生错误。我的对象的文件名也与类名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

2 个答案:

答案 0 :(得分:1)

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

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类中的所有方法,看看它是否仍然无法实例化。可能是你的班级有一些语法错误。

答案 1 :(得分:1)

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;

试试这个