As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center用于指导。
6年前关闭。
我知道<?php include 'filename.php';?>可以用于跨多个页面复制静态内容。
我以前在一些网站上做过这个,但是我想做一个更专业的网站,我不确定在很多<?php include上打点是不是一个好办法,感觉像胶水。
专业人士就是这样做的吗?我这么问是因为我的所有页面的标题、页眉、页脚和导航都有一个include
如果没有更好或更干净的方式,我会继续,但如果有更好的方式,人们管理这一点我想知道。
干杯

最佳答案

很多专业人员使用模板库。这是一种更面向对象的方式来做你所说的事情。其思想是,您可以将内容分解为更小的可重用部分,以及一些添加动态内容的方法,然后构建一个web页面就非常简单了。
如果您想创建自己的php类,请考虑如下所示:

<?php

class Link
{
public $relation;
public $type;
public $href;

public function __construct($relation, $type, $href)
{
    $this->relation = $relation;
    $this->type = $type;
    $this->href = $href;
}

public function __toString()
{
    return "<link rel='$this->relation' type='$this->type' href='$this->href'>";
}
}

class Anchor
{
public $href;
public $content;

public function __construct($href, $content)
{
    $this->href = $href;
    $this->content = $content;
}

public function __toString()
{
    return "<a href='$this->href'>$this->content</a>";
}
}

class Script
{
public $source;
public $type;

public function __construct($source, $type)
{
    $this->source = $source;
    $this->type = $type;
}

public function __toString()
{
    return "<script src='$this->source' type='$this->type'></script>";
}
}

class Option
{
public $value;
public $text;

public function __construct($value, $text)
{
    $this->value = $value;
    $this->text = $text;
}

public function __toString()
{
    return "<option value='$this->value'>$this->text</option>";
}
}

class Select
{
public $options;
public $name;

public function __construct($name, $options)
{
    $this->name = $name;
    $this->options = $options;
}

public function __toString()
{
    $result = "<select id='$this->name'>";
    foreach($this->options as $option)
        $result.=$option;
    $result.= "</select>";
    return $result;
}
}

class Div
{
public $id;
public $class;
public $content;

public function __construct($id, $class, $content)
{
    $this->id = $id;
    $this->class = $class;
    $this->content = $content;
}

public function __toString()
{
    return "<div id='$this->id' class='$this->class'>$this->content</div>";
}
}


class HeaderItem
{
public $title;
public $links;

public function __construct($title, $links)
{
    $this->title = $title;
    $this->links = $links;
}

public function __toString()
{
    $result = "<head>"."<title>$this->title</title>";
    foreach($this->links as $link)
        $result.= $link;
    $result.= "</head>";
    return $result;
}
}

class Document
{
public $header;
public $body;

public function __construct($header, $body)
{
    $this->header = $header;
    $this->body = $body;
}

public function __toString()
{
    return "<!DOCTYPE HTML>$this->header<html><body>$this->body</body></html>";
}

}
?>

09-11 20:27
查看更多