note 104886 deleted from language.namespaces.global by thiago
| From: | thiago@php.net | Date: | Wed, 13 Jul 2011 16:53:36 +0000 |
| Subject: | note 104886 deleted from language.namespaces.global by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181463@lists.php.net to get a copy of this message | ||
Note Submitter: dave [at] unluckypixie [full stop] com
----
I want to define two classes in one file (horrible I know but I have no choice using the framework I
am in) but it doesn't seem possible to switch the namespace back to the global one, or at least
I can't find in the documentation how to do it, e.g.
Define these classes:
\Db\Connection (i.e. Connection class in Db Namespace)
\Db_Connection (i.e. Db_Connection class in global namespace)
it should be simple but I can't find the syntax:
<?
namespace Db;
class Connection
{
}
namespace <WHAT TO PUT HERE?!>;
class Db_Connection extends \Db\Connection
{
}
?>
I have tried "global" "Global" "\" and "" and nothing at all
- none of these seem to work. The only way I have found is that you can do this:
namespace {
class Db_Connection extends \Db\Connection
{
}
}
Which is great but PHP complains if you mix the syntaxes in a file so I would also have to have that
namespace syntax around the other class and we are not using it elsewhere so I don't want to.
If you could declare classes with namespaces on them this would be easy! But for some reason you
can't do that, e.g. why can't I do this:
<?
class \Db\Connection
{
}
class \Db_Connection extends \Db\Connection
{
}
?>
That would be nicer.
The best solution I have come up with is to put them into separate files the first includes the
second, the second one then doesn't have a namespace definition, but I really feel I
shouldn't have to do that:
<?
namespace Db;
class Connection
{
}
include "Db_Connection";
?>
<?
class Db_Connection extends \Db\Connection
{
}
?>