简体   繁体   English

PHP无法重新声明由函数引起的类

[英]PHP Cannot redeclare class caused by a function

I am getting a Cannot redeclare class error. 我收到无法重新声明类错误。 This is the class file that is causing the error 这是导致错误的类文件

<?
class siteLoader{


public $view;
protected $page;
//vars for render head function
protected $arraySize;
protected $counter;

public function __construct(){}
//renders the view for the site
public function renderView($view){
    $this->view = $view;
    return "view/{$view}.view.php";
}
//renders the head part of the site
//public function renderHead($head){
public function renderHead($charset,$title,$description,$keywords,$applicationName,$css){
    //$this->head = $head;
    $this->charset = $charset;
    $this->title = $title;
    $this->description = $description;
    $this->keywords = $keywords;
    $this->applicationName = $applicationName;
    $this->css = $css;
    $arraySize = count($this->css, COUNT_RECURSIVE);
    $counter =0;
    echo '<head>
        <meta charset="'.$charset.'">
        <meta application-name="'.$applicationName.'">
        <meta keywords="'.$keywords.'">
        <meta description="'.$description.'">
        <title>'.$title.'</title>
    ';
    do{
        echo '<link type="text/css" rel="stylesheet" href="css/'.$this->css[$counter].'.css">';
        $counter++;
        }while($counter != $arraySize);
    echo '</head>';
    return 0;
}


}
?>

The error only happens when I call for the renderView() function. 仅当我调用renderView()函数时,才会发生该错误。 Below here is the index where I call for the class and the function. 下面是我调用类和函数的索引。

<!DOCTYPE html>
<html>
<?
if(!class_exists('siteLoader'))
{
require_once('/classes/html.class.php');
}

//$head = new html;
$html = new siteLoader;
$viewrender = new siteLoader;

$charset = "utf-8";
$title = "musicDB";
$description = "Not made yet";
$keywords = "music,DB,rock,Database,etc";
$applicationName = "Music DataBase";
$css = array('style');
$view = "main";
     $html->renderHead($charset,$title,$description,$keywords,$applicationName,$css);
include $viewrender->renderView($view);

?>
</html>

As seen above I use require_once and check if the class is already loaded. 如上所示,我使用require_once并检查该类是否已加载。 The problem is probably in the renderView function because when i don't use that function everything works. 问题可能出在renderView函数中,因为当我不使用该函数时,一切正常。

what this line do include $viewrender->renderView($view); 这行内容包括$ viewrender-> renderView($ view); what is the name of the class in main.view.php see if the class name is siteloader main.view.php中该类的名称是什么,请查看该类名称是否为siteloader

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

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