什么';这个代码错了吗?PHP与OOP


What's wrong with this code? PHP with OOP

<?php
    class Model
    {
        private $title = "Please define page's title";
        private $body = "Please define page's body";
        private $footer = "Please define page's footer";
        public function setTitle($title)
        {
            $this->title = $title;
        }
        public function setContent($content)
        {
            $this->body = $content;
        }
        public function setFooter($footer)
        {
            $this->footer = $footer;
        }
        public function showTitle()
        {
            return $this->title;
        }
        public function showContent()
        {
            return $this->body;
        }
        public function showFooter()
        {
            return $this->footer;
        }
    }
    class WebPage extends Model
    {
        function __construct()
        {
            echo '<html>';
            echo '<head>';
            echo '<title>';
            echo $this->showTitle();
            echo '</title>';
            echo '</head>';
            echo '<body>';
            echo $this->showContent();
            echo $this->showFooter();
            echo '</body>';
            echo '</html>';
        }
    }
?>
//In the indeex file
<?php
    $model = new Model();
    $model->setTitle("Php OOP Test");
    $model->setContent("HelloWorld");
    $model->setFooter("Hello");
    $page = new WebPage();
?>

问题是所有的标题、内容和页脚都没有改变。

我尝试构建自己的PHP框架。

这看起来有点愚蠢,但我对这种事情还不熟悉。

这就是你要做的,你试图做的

class Model
{
    private $title = "Please define page's title";
    private $body = "Please define page's body";
    private $footer = "Please define page's footer";
    public function setTitle($title)
    {
        $this->title = $title;
    }
    public function setContent($content)
    {
        $this->body = $content;
    }
    public function setFooter($footer)
    {
        $this->footer = $footer;
    }
    public function showTitle()
    {
        return $this->title;
    }
    public function showContent()
    {
        return $this->body;
    }
    public function showFooter()
    {
        return $this->footer;
    }
}
class WebPage extends Model
{
    public function output() {
        echo '<html>';
        echo '<head>';
        echo '<title>';
        echo $this->showTitle();
        echo '</title>';
        echo '</head>';
        echo '<body>';
        echo $this->showContent();
        echo $this->showFooter();
        echo '</body>';
        echo '</html>';
    }
}
$page = new WebPage();
$page->setTitle("Php OOP Test");
$page->setContent("HelloWorld");
$page->setFooter("Hello");
$page->output();

如注释中所述,您在示例中实例化了两个不同类的两个独立实例。由于WebPage正在扩展Model,因此您只需要一个WebPage实例。从那里,我将echo内容移动到Webpage的一个单独的公共方法中,这样在修改Model属性

后就可以调用它

您正在创建页面的新对象,该对象是$page = new WebPage();模型的子对象,因此您的新对象将具有此变量

private $title = "Please define page's title";
    private $body = "Please define page's body";
    private $footer = "Please define page's footer";

您可以在另一个对象上设置诸如标题和内容等变量,该对象是$model

以下是正确的实现。

由于WebPage在中扩展了Model,继承了它的所有函数和变量,因此我们制作了WebPage的新实例,然后用该对象设置titlebodyfooter。然后,我们调用特定于WebPage类的showPage,该类将输出我们设置的所有变量。

<?php
    class Model
    {
        private $title = "Please define page's title";
        private $body = "Please define page's body";
        private $footer = "Please define page's footer";
        public function setTitle($title)
        {
            $this->title = $title;
        }
        public function setContent($content)
        {
            $this->body = $content;
        }
        public function setFooter($footer)
        {
            $this->footer = $footer;
        }
        public function showTitle()
        {
            return $this->title;
        }
        public function showContent()
        {
            return $this->body;
        }
        public function showFooter()
        {
            return $this->footer;
        }
    }
    class WebPage extends Model
    {
        function showPage()
        {
            echo '<html>';
            echo '<head>';
            echo '<title>';
            echo $this->showTitle();
            echo '</title>';
            echo '</head>';
            echo '<body>';
            echo $this->showContent();
            echo $this->showFooter();
            echo '</body>';
            echo '</html>';
        }
    }
?>
//In the index file
<?php
    $page = new WebPage();
    $page->setTitle("Php OOP Test");
    $page->setContent("HelloWorld");
    $page->setFooter("Hello");
    $page->showPage();
?>