开发者

Unwanted repeating html result from PHP

开发者 https://www.devze.com 2023-01-03 20:20 出处:网络
I don\'t know what is wrong with this code, it is a bug or I made a mistake somewhere; xDebug show nothing.

I don't know what is wrong with this code, it is a bug or I made a mistake somewhere; xDebug show nothing.

Class script

class theme {
    function theme() {
        //show header (meta, style, htmldoctype, script, and title) 
        $this->htmlheader();

        //show main content
        //show footer
    }


    function htmlheader() {
        require "localsettings.php";
        echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n<head>\n";
        echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";

        echo "<title>$site_name - $page_title</title>\n";
        echo "</head>\n";
    }
}

index.php

require "theme.class.php";
$html = new theme();
//display result
$html->theme();

Output (incorrectly repeated)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<开发者_StackOverflow中文版;meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>site title - </title>
</head>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>site title - </title>
</head>


When you name the function the same as the class, it is a "constructor", and gets called when the class is instantiated. Thus, your function theme() is called both here:

$html = new theme();

and here:

$html->theme();

Remove the latter, and you should be good to go.

0

精彩评论

暂无评论...
验证码 换一张
取 消