【问题标题】:Does reflection class have implicit method __toString() in php?反射类在php中有隐式方法__toString()吗?
【发布时间】:2020-12-24 16:34:18
【问题描述】:

我正在阅读https://www.amazon.com/PHP-MySQL-Web-Development-4th/dp/0672329166 .在 OOP 章节中我实现了page 类(代码如下),然后在另一个脚本中尝试通过Reflection("Page") 创建一个实例,然后尝试echo 该类(通过隐式方法@ 987654325@,和书上一样:

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, $url, $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,通过Reflaction类推断出Page类?

【问题讨论】:

    标签: php class oop tostring


    【解决方案1】:

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

    <?php
    
    // ...
    
    $reflector = new ReflectionClass('Page');
    
    $properties = $reflector->getProperties();
    
    foreach($properties as $property)
    {
        echo $property->getName() . "\n";
    }
    

    【讨论】:

    • 我还没有找到任何更好的涵盖 mysql 的书籍。这本书有 5 年历史了,是的,它很旧,但是现在很少有 php 书籍,因为今天没有人使用 php。
    • 您可以尝试阅读 Josh Lockhart 的 phptherightway.com 和“现代 PHP”。这个页面也是一个好的开始Learn modern PHP。我也写了一些关于这个话题的blog posts
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-11-06
    • 2014-05-15
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多