Solved

why can I not put a namespace in a file without including it?

Posted on 2014-04-22
2
196 Views
Last Modified: 2014-04-23
<?php
namespace baseFile{
  class inBaseFile{
    function __construct($who){
      echo "<br>Hello $who i am class: ".__CLASS__." namespace: ".__NAMESPACE__."<br>";
    }
    
  }
}
require_once('tasks/business.php');
class fred
{
  function __construct($who)
  {
    echo "Hullo $who I am fred";
  }
}
class james
{
  function __construct($who){
    echo "<br>Hello $who I am ".__CLASS__."<br>";
  }
}


$x = new fred('inside');
$y = new \business\monkey('default');
$z= new james('barbara');
?>

Open in new window



when namespace basefile is at the bottom
error: namespace must be qualified at the top

when namespace is at the top:
error: all code must be in namespace



<?php
namespace business {
  class monkey
  {
    function __construct($who)
    {
      echo "Hello $who from monkey";
    }
  }
}
?>

Open in new window


how is putting namespace in running file any different than creating another file and require_once('tasks/business.php');
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
acbxyz earned 500 total points
ID: 40015409
If you use namespaces in a file everything must be in a namespace. This does not mean everything must be in the same namespace.
http://www.php.net/manual/en/language.namespaces.definitionmultiple.php
0
 

Author Closing Comment

by:rgb192
ID: 40019109
namespace { // global code
session_start();
$a = MyProject\connect();
echo MyProject\Connection::start();
}

thanks

everything must be in a namespace, if there is 1+ namespace
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question