微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反射类在PHP中是否具有隐式方法__toString?

如何解决反射类在PHP中是否具有隐式方法__toString?

我正在阅读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>&copy; 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呢?

解决方法

这本书和示例非常过时,不建议在2020年这样开发。代码中充满了XSS安全问题和不良的代码风格。无论如何,您可以在这里尝试以反映课程:

<?php

// ...

$reflector = new ReflectionClass('Page');

$properties = $reflector->getProperties();

foreach($properties as $property)
{
    echo $property->getName() . "\n";
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。