Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-22
2
Medium Priority
?
201 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 look for a specific file type in a local or remote server directory using PHP.

670 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