tags:

views:

41

answers:

1

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\"&gt;\n&lt;html&gt;\n&lt;head&gt;\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"&gt;
<html>
<head>
<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"&gt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>site title - </title>
</head>
+10  A: 

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.

Peter Anselmo
Note that in PHP5 you could use `__construct` for the constructor (more explicit=better imo).
ChristopheD
@ChristopheD It shouldn't be a big surprise though; it's pretty common in other languages for the class name to represent a constructor
Michael Mrozek