note 104886 added to language.namespaces.global

From: Date: Wed, 13 Jul 2011 15:38:57 +0000
Subject: note 104886 added to language.namespaces.global
Groups: php.notes 
Request: Send a blank email to php-notes+get-181462@lists.php.net to get a copy of this message
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 { } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 89.206.182.27 ---- Manual Page -- http://www.php.net/manual/en/language.namespaces.global.php Edit -- https://master.php.net/note/edit/104886 Del: integrated -- https://master.php.net/note/delete/104886/integrated Del: useless -- https://master.php.net/note/delete/104886/useless Del: bad code -- https://master.php.net/note/delete/104886/bad+code Del: spam -- https://master.php.net/note/delete/104886/spam Del: non-english -- https://master.php.net/note/delete/104886/non-english Del: in docs -- https://master.php.net/note/delete/104886/in+docs Del: other reasons-- https://master.php.net/note/delete/104886 Reject -- https://master.php.net/note/reject/104886 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#181462) next »