我是一位正在嘗試使用OOP和PHP的大學生。我想使用BuildPage類建立一個頁面。使用PHP類生成網頁
<?php
Class BuildPage {
private $title;
private $style;
private $head;
private $header;
private $page;
private $footer;
private $finalPage;
public function __construct($title, $style)
{
$this->title = $title;
$this->style = $style;
}
public function addHead()
{
$this->head = "
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>$this->title</title>
<link href='$this->style' rel='stylesheet' type='text/css'>
<link rel='stylesheet' href='style.css'>
</head>";
}
public function addToPage($partOfPage)
{
if(empty($this->page) || !isset($this->page))
{
$this->page = $partOfPage;
}
else {
$this->page .= "<br> " . $partOfPage;
}
}
public function addHeader($header)
{
$this->header = "<body><div class='container'><header> " . $header . "
</header>";
}
public function addFooter($footer)
{
$this->footer .= "<BR><footer> " . $footer . " </footer></div></body>
</html>";
}
public function getPage()
{
$this->finalPage = $this->head . $this->header . $this->page .
$this->footer;
return $this->finalPage;
}
}
?>
然而,當我嘗試使用功能來構建頁面,我不明白如何在參數中使用PHP如下:
$buildPage->addToPage("
<!-- Here is our page's main content -->
<!-- Use function to display songs -->
<?php $music->displaySongs('xmlfiles/songs.xml'); ?>
");
如果我逃避$如\ $嘗試並使其成爲一個字符串,出於某種原因,它成爲HTML中的評論。這種方法有可能工作嗎?
非常感謝
編輯:
這是下面的索引頁,我調用類的帶有回聲底部。
<?php
require_once('phpclasses/Connect.php');
require_once('phpclasses/BuildPage.php');
require_once('phpclasses/MusicIE.php');
$dbconnect = new Connect();
$music = new MusicIE();
$buildPage = new BuildPage("Music Website", "style.css");
$buildPage->addHead();
$buildPage->addHeader("
<!-- Here is the main header that is used accross all the pages of my
website
-->
<div class='PageHeading'>Available Music Listed Below:</div>
<nav>
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Register</a></li>
<li><a href='#'>Login</a></li>
</ul>
</nav>
<form>
<input type='search' name='q' placeholder='Search query'>
<input type='submit' value='Go!'>
</form>
");
$buildPage->addToPage("
<!-- Here is our page's main content -->
<!-- Use function to display songs -->
" . $music->displaySongs('xmlfiles/songs.xml'));
$buildPage->addFooter("
<!-- And here is my footer that is used across all the pages of our website
-->
<p>©Copyright 2017 by Kris Wilson. All rights reversed.</p>
");
echo $buildPage->getPage();
?>
我編輯了我原來的問題,如果我按照你的建議去做,它會在頁面頂部顯示HTML以外的結果 – Sifer
alter $ music-> displaySongs('xmlfiles/songs.xml'))不會回顯字符串,而是返回它 –