Fatal error: Uncaught Error: Class "bob\html\page\Page" not found in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/importing-and-aliasing-namespace-pag-198/index.php:4 Stack trace: #0 {main} thrown in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/importing-and-aliasing-namespace-pag-198/index.php on line 4
index.php

<?php
include "Html.php";
use bob\html\page as www;
$table = new www\Page();
$table->title = "My table";
$table->numRows = 5;
?>

<html>
<body>

<?php $table->message(); ?>

</body>
</html>

Html.php

<?php
namespace Html;
class Page {
    public $title = "";
    public $numRows = 0;

    public function message() {
        echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>";
    }
}

class Row {
    public $numCells = 0;
    public function message() {
        echo "<p>The row has {$this->numCells} cells.</p>";
    }
}
?>

    Where is your bob\html\page namespace declared? Can't use it if it can't be found.

    now it works I going to do the second example thank you weedpacket
    use bob\html\page;
    $services = new page\Page()

    use bob\html\page as www;
    $services = new www\Page();
    index.php

    <?php
    include "html.php";
    use bob\html\page ;
    $table = new page\Page();
    $table->title = "My table";
    $table->numRows = 5;
    ?>
    
    <html>
    <body>
    
    <?php $table->message(); ?>
    
    </body>
    </html>

    html.php

    <?php
    namespace bob\html\page;
    class Page {
        public $title = "";
        public $numRows = 0;
    
        public function message() {
            echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>";
        }
    }
    
    ?>

      Or alternatively, in html.php:

      namespace bob\html;

      And in index.php:

      use bob\html\Page;
      $table = new Page();

        Write a Reply...