Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

758 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

21 Experts available now in Live!

Get 1:1 Help Now