Skip to content
Advertisement

Does reflection class have implicit method __toString() in php?

I am reading https://www.amazon.com/PHP-MySQL-Web-Development-4th/dp/0672329166 . And there is OOP chapter where I have implemented the page class (code below), and then in another script try to make an instance through Reflection("Page"), where in turn trying to echo that class (via the implicit method __toString(), just as in the book:

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
        }
    }
}

The class by itself is not important, but the attempt to infer it from Reflection class is:

foo.php:

<?php
require_once('page.php');
$class = new Reflection('Page');
echo '<pre>'.$class.'</pre>';

gives error:

Recoverable fatal error: Object of class Reflection could not be converted to string in /var/www/html/2/foo.php on line 5

So the error says the Reflection class does not have the method __toString(), but since it is in the book as example, I assume it should. So how to echo the variable $class, which is infered Page class via Reflaction class?

Advertisement

Answer

This book and the example is very outdated and it’s not recommended developing like this in 2020. The code is full of XSS security issues and bad code style. Anyway, you can try this here to reflect the class:

<?php

// ...

$reflector = new ReflectionClass('Page');

$properties = $reflector->getProperties();

foreach($properties as $property)
{
    echo $property->getName() . "n";
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement