问题描述
我正在阅读https://www.amazon.com/PHP-MySQL-Web-Development-4th/dp/0672329166
。在OOP的一章中,我实现了page
类(下面的代码),然后在另一个脚本中尝试通过Reflection("Page")
创建一个实例,然后在其中尝试对echo
进行创建。 (通过隐式方法__toString()
,就像在书中一样:
page.php :
<?php
class Page
{
public $content;
public function setContent(string $c)
{
$this->content = $c;
}
public $title = "Papaluz Corp";
public function setTitle(string $t)
{
$this->title = $t;
}
public $keywords = " Papaluz Consulting,Three Letter Abbreviation,some of my best friends are search engines";
public function setKeywords(string $k)
{
$this->keywords = $k;
}
public $buttons = [
'Home' => 'home.php','Contact' => 'contact.php','Services' => 'services.php','About' => 'about.php'
];
public function setButtons(array $b)
{
$this->buttons = $b;
}
public function display()
{
echo "<html>\n<head>\n";
$this->displayTitle();
$this->displayKeywords();
$this->displayStyles();
echo "</head>\n<body>\n";
$this->displayHeader();
$this->displayMenu($this->buttons);
echo $this->content;
$this->displayFooter();
echo "</body>\n</html>\n";
}
public function displayTitle()
{
echo '<title>' . $this->title . '</title>';
}
public function displayKeywords()
{
echo '<meta name="keywords" content="' . $this->keywords . '"/>';
}
public function displayStyles()
{
?>
<link href="/2/default/style.css" rel="stylesheet">
<?php
}
public function displayHeader()
{
?>
<header>
<img src='/data/img/pap_white.png' width="130" height="130">
<h1>Papaluz Consulting</h1>
</header>
<?php
}
public function displayMenu(array $buttons)
{
echo '<nav>';
foreach ($buttons as $name => $url) {
$this->displayButton($name,$url,!$this->isUrlCurrentPage($url));
}
echo "</nav>\n";
}
public function displayFooter()
{
?>
<footer>
<p>© Papluz consulting Corp.<br>
please see our<a href='legal.php'>legal information page</a>.
</p>
</footer>
<?php
}
public function isUrlCurrentPage($url)
{
if (strpos($_SERVER['PHP_SELF'],$url) === false) {
return false;
} else {
return true;
}
}
public function displayButton($name,$active = true)
{
if ($active) {
?>
<div class="menuitem">
<a href="<?= $url ?>">
<img src='/data/img/arrow_white.svg' width="20" height="20">
<span class='menutext'><?= $name ?></span>
</a>
</div>
<?php
} else {
?>
<div class="menuitem">
<!-- <img src='/data/img/arrow_white.svg'> -->
<span class='menutext'><?= $name ?></span>
</div>
<?php
}
}
}
该类本身并不重要,但是从Reflection
类推断出它的尝试是:
foo.php :
<?php
require_once('page.php');
$class = new Reflection('Page');
echo '<pre>'.$class.'</pre>';
给出错误:
Recoverable fatal error: Object of class Reflection could not be converted to string in /var/www/html/2/foo.php on line 5
因此错误表明Reflection
类没有方法__toString()
,但是由于它作为示例在书中,因此我认为应该有。那么如何通过$class
类来推断Page
类的变量Reflaction
呢?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)