Fatal error: Uncaught Error: Class "bob\html\page\bob\html\page\Page" not found in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-subnamespaces-pag-197/using-subnamespaces.php:6 Stack trace: #0 {main} thrown in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-subnamespaces-pag-197/using-subnamespaces.php on line 6

namespace bob\html\page;
class Page {
    //..
}
$services = new bob\html\page\Page();
//$service = new html\page\Page();

    The problem is that you are already in that namespace. Typically, you would define the class in one name-spaced file, then in another file where you have included that class definition file, you would then use the namespace (since in that calling file you are not already in that namespace). It is, however, possible to do what you tried by using the bracket notation to control which code is within that name-space:

    namespace bob\html\page {  // note use of brackets
      class Page {
          //...
      }
    }
    // back in default name-space now
    $test = new bob\html\page\Page();
    

      Fatal error: No code may exist outside of namespace {} in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-subnamespaces-pag-197/using-subnamespaces.php on line 8

      namespace bob\html\page{
          class Page {
              //..
          }
      
      }
      $services = new bob\html\page\Page();

        Fatal error: No code may exist outside of namespace {} in /var/www/html/PHP-and-MySQL-Web-Development/Chapter06/using-subnamespaces-pag-197/using-subnamespaces.php on line 17

        namespace bob\html\page{
            
        
            class Page {
                //..
            }
        
        }
        namespace {
            $services = new bob\html\page\Page();
        }

        bertrc on line 17

        What is on line 17? According to the error message, code outside of the namespace.

          <?php
          namespace bob\html\page {
              class Page
              {
                  //..
              }
          
          }
              namespace {
                  $services = new bob\html\page\Page();
              }

          I have deleted ?> in the final line now is working
          phpstorm said before
          Global code should be enclosed in global namespace declaration
          line 13

          bertrc I have deleted ?>

          FYI, it is usually best to not put in the optional closing PHP tag at the very end of the file, especially if the file only contains PHP code (i.e. it does not alternate in and out of PHP and HTML modes). If you include the closing tag and then inadvertently have anything, even just a space or a newline, after it; then any code that includes that file may fail if the calling file uses any function that can only operate before HTTP headers are sent -- which can be caused by those trailing characters. (Similarly, you normally want to avoid anything before the opening PHP tag on such files, as well.)

            Write a Reply...