Jump to content

Recommended Posts

Hi

 

I have just recently tried to incorporate namespaces into my framework but having trouble understanding how I should use them in my project.

 

I have a class called Foo which is part of the namespace \CompanyName\Project.

 

namespace \CompanyName\Project;

class Foo {

}

 

I have a file called index.php. index.php needs to use that class. index.php handles the autoloading of that class (ive omitted the autoloading code). My index.php looks like below.

 

$foo = new Foo();

 

This code fails because it can't find class Foo. I can make it work by using the fully qualified name instead.

 

$foo = new \CompanyName\Project\Foo();

 

I can also make it work by making index.php part of \Company\Project\ namespace.

 

namespace \CompanyName\Project;
$foo = new Foo();

 

Basically my problem is that I have lots of different file includes for individual pages on my site. They all use the old unqualifed class names which are not getting resolved by PHP because it can no longer find them as all of the classes now belong to a namespace. Do I

 

A - Go through every included file and change every class name to the fully qualifed class name?

B - Go through every included file and make every file part of the \CompanyName\Project namespace (This feels like the wrong solution)?

C - Make PHP treat the old unqualifed class name as a global through use of the 'use' statement. I don't know how to do this. The trouble is there could be 10 - 20 different

classes per file which would mean 20 different use declarations. Ideally it would be great if I could import an entire namespace into the global namespace like "use \CompanyName\Project as \;".

 

Basically I want a way to move all classes and functions from a particular namespace into the global namespace so I can use the unqualifed class names throughout the included files. And I want to be able to do it from one file, index.php. I don't think this is possible though as PHP does state

 

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

 

I think it will have to be option A , before I begin, am I on the right track?

Link to comment
https://forums.phpfreaks.com/topic/268813-php-namespace-help/
Share on other sites

There's no point in using namespaces if there's only one or two in the entire project. Just do without it and consider them in the global namespace (which is exactly the end result).

 

Most of the time, if you're organizing classes into multiple folders (eg, lots of underscores as separators) then you can use namespaces. Or you don't and just stick with long class names; PHP's implementation of namespaces can be a bit awkward at times* so it's not uncommon to see PHP 5.3+ code that isn't using namespaces.

 

* Can't import/use entire namespaces, class names resolve differently than functions and constants, the ridiculous notion that a backslash is the right separator character...

Link to comment
https://forums.phpfreaks.com/topic/268813-php-namespace-help/#findComment-1380993
Share on other sites

Hi

 

Yes Ive been testing locally and reading further and stumbled upon this article. That article highlights all the pitfalls you will encounter with PHP namespaces which I just learnt myself. After the require_once / include_once debacle (before autoloading) you would have thought they would have at least supported some kind of command to put entire namespaces into the global namespace or be able to wildcard a namespace to make it easier. Unfortuantely I have come across a part of my project which will require namespaces so I now have to make the decision of

 

Using fully qualifed class names throughout my code.

Creating a 'use' list at the top of every included file.

 

I will probably go with fully qualifed space names. Netbeans autocomplete makes this ideal and it saves having to maintain a 'use' list (which resembles a list of require_onces I used to see in my code) at the top of every included file. Thanks for your input.

Link to comment
https://forums.phpfreaks.com/topic/268813-php-namespace-help/#findComment-1380995
Share on other sites

I don't mind a list of use statements: it reminds me about classes I'm using that are in another namespace. Besides, most of the time (1) I'm using classes in the same namespace so the list is fairly small and (2) if I need multiple classes outside the namespace there's a good chance I need to refactor a little for the sake of encapsulation and coupling and whatnot.

Link to comment
https://forums.phpfreaks.com/topic/268813-php-namespace-help/#findComment-1381165
Share on other sites

I went with fully qualified in the end. Took a day but I got the job done :tease-01:. At the start of the project I opted not to use a starting class name like Project_* to save typing. If I had Netbeans could of done a search and replace in 10 seconds :sweat: , Oh well.

Link to comment
https://forums.phpfreaks.com/topic/268813-php-namespace-help/#findComment-1381196
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.