Solved

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

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

710 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