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

I have the file page.php in the folder, I must use the __toString() method but I don't know how

Fatal error: Uncaught Error: Object of class Reflection could not be converted to string in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-the-reflection-api-pag-194/using-the-reflection-api.php:4 Stack trace: #0 {main} thrown in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-the-reflection-api-pag-194/using-the-reflection-api.php on line 4

phpstorm return this errors
Method '__toString' is not implemented for '\Reflection'
Method call is provided 1 parameters, but the method signature uses 0 parameters

    Why do you want to echo an object? If you just want to verify that PHP "works", you could do something like this, I suppose:

    echo "<pre>".var_export($class, true)."</pre>";
    

    PS: I have never used Reflection in anything I've worked on, so I personally don't recommend spending too much time learning it if you don't have a specific need for it, with all the other more common functionality you have to learn. 😉

    I am learning from a book is just one example I am not going to spent much time but I try to do all the exercises from the book
    thank you

    returns this message

    Reflection::__set_state(array(
    ))

    it doesn't work well

    page.php

    <?php
    class Page
    {
      // class Page's attributes
      public $content;
      public $title = "TLA Consulting Pty Ltd";
      public $keywords = "TLA Consulting, Three Letter Abbreviation, 
                         some of my best friends are search engines";
      public $buttons = array("Home"   => "home.php", 
                            "Contact"  => "contact.php", 
                            "Services" => "services.php", 
                            "Site Map" => "map.php"
                        );
    
      // class Page's operations
      public function __set($name, $value)
      {
        $this->$name = $value;
      }
    
      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="styles.css" type="text/css" rel="stylesheet">
        <?php
      }
    
      public function DisplayHeader()
      { 
        ?>   
        <!-- page header -->
        <header>    
          <img src="logo.gif" alt="TLA logo" height="70" width="70" /> 
          <h1>TLA Consulting</h1>
        </header>
        <?php
      }
    
      public function DisplayMenu($buttons)
      {
        echo "<!-- menu -->
        <nav>";
    
        while (list($name, $url) = each($buttons)) {
          $this->DisplayButton($name, $url, 
                   !$this->IsURLCurrentPage($url));
        }
        echo "</nav>\n";
      }
    
      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="s-logo.gif" alt="" height="20" width="20" />
            <span class="menutext"><?=$name?></span>
            </a>
          </div>
          <?php
        } else { ?>
          <div class="menuitem">
          <img src="side-logo.gif">
          <span class="menutext"><?=$name?></span>
          </div>
          <?php
        }  
      }
    
      public function DisplayFooter()
      {
        ?>
        <!-- page footer -->
        <footer>
          <p>&copy; TLA Consulting Pty Ltd.<br />
          Please see our 
          <a href="legal.php">legal information page</a>.</p>
        </footer>
        <?php
      }
    }
    ?>

      Maybe try using ReflectionClass instead of Reflection, since ReflectionClass does have __toString() defined?

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

      But without knowing what it is you are actually trying to accomplish, that's just speculation on my part.

        Are you reading the rest of the book as well? Presumably the exercises are there for you to check that you've understood the text (I haven't looked at the book.).

          return this message but I should return all the file page.php
          Reflection::__set_state(array(
          ))

          bertrc
          Doesn't sound like reflection has anything to do with what you're trying to do. Reflection isn't for generating pages, it's for examining the interior workings of PHP code (like finding out what parameters a function takes).

          How about you post the text of the exercise? That might make more sense.

            I have only this information of the book.
            but I think this code is for php 7 I have in my computer php 8.1.
            return this message but I should return all the file page.php, more information.
            Reflection::__set_state(array(
            ))

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

            page.php

            <?php
            class Page
            {
              // class Page's attributes
              public $content;
              public $title = "TLA Consulting Pty Ltd";
              public $keywords = "TLA Consulting, Three Letter Abbreviation, 
                                 some of my best friends are search engines";
              public $buttons = array("Home"   => "home.php", 
                                    "Contact"  => "contact.php", 
                                    "Services" => "services.php", 
                                    "Site Map" => "map.php"
                                );
            
              // class Page's operations
              public function __set($name, $value)
              {
                $this->$name = $value;
              }
            
              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="styles.css" type="text/css" rel="stylesheet">
                <?php
              }
            
              public function DisplayHeader()
              { 
                ?>   
                <!-- page header -->
                <header>    
                  <img src="logo.gif" alt="TLA logo" height="70" width="70" /> 
                  <h1>TLA Consulting</h1>
                </header>
                <?php
              }
            
              public function DisplayMenu($buttons)
              {
                echo "<!-- menu -->
                <nav>";
            
                while (list($name, $url) = each($buttons)) {
                  $this->DisplayButton($name, $url, 
                           !$this->IsURLCurrentPage($url));
                }
                echo "</nav>\n";
              }
            
              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="s-logo.gif" alt="" height="20" width="20" />
                    <span class="menutext"><?=$name?></span>
                    </a>
                  </div>
                  <?php
                } else { ?>
                  <div class="menuitem">
                  <img src="side-logo.gif">
                  <span class="menutext"><?=$name?></span>
                  </div>
                  <?php
                }  
              }
            
              public function DisplayFooter()
              {
                ?>
                <!-- page footer -->
                <footer>
                  <p>&copy; TLA Consulting Pty Ltd.<br />
                  Please see our 
                  <a href="legal.php">legal information page</a>.</p>
                </footer>
                <?php
              }
            }
            ?>
              Write a Reply...