Solved

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

Posted on 2014-04-22
2
190 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now