tags:

views:

81

answers:

2

I have a significant number of object libraries written for PHP 5.2.5, and I'm trying to weigh the benefits of retrofitting them for namespaces. I don't have any concerns about the server PHP version at the moment, since any relevant machines are under my control, so I'm not worried about backwards compatibility. As far as the structure of the libraries, I use the same convention as Zend Framework, (Library_Module_Class_Name e.g.) so I don't currently have any naming conflicts internal to the libraries. I'd anticipate moving the Library and Module parts of those classnames to namespaces.

That said, if the code is already written, is there any good reason to move over to namespaces?

Thanks, Joe

A: 

Yes, your object is an Object not a Namespace_Prepended_To_An_Object. Wouldn't you rather work with a Controller rather than a Zend_Controller? The latter just looks ridiculous.

blockhead
I feel like this isn't a response to the question....
mattbasta
This is the point of of namespaces in the first place.
blockhead
+3  A: 

If you're looking for technical reasons, I don't think there's anything to justify the cost of a large code move. As far as I'm aware, namespaces don't confer any kind of technical advantage or performance benefit. It's easier to write decent autoloading functions for a well-structured set of namespaced PHP code, but other than that, I can't think of anything that would be a ground-shaking definitive reason to start converting. If anything, there's probably a tiny bit of overhead for namespace resolution (benchmarks would be neat to see, but I'm sure it's a negligible difference).

That being said, I've recently started updating an old set of code to use namespaces, and I'm enjoying the organizational capability that it provides. The code has a tendency to be a bit more verbose, but I switched IDEs to Netbeans 6.8 at the start of the upgrade, and the autocomplete takes care of a lot of the tediousness of importing and references namespaces. The namespace support impressed me, actually. It's made the whole project quite easy, and actually, dare I say it, fun.

zombat
So namespacing has actually made your code *more* verbose? Is this just a matter of the "use X" statements, or something else? Can you give me a little more detail on that? Thx!
Joseph Mastey
Oh, sure. I didn't use a Zend-style naming scheme before, so that's probably a big part of it. I had a complex auto-loading function that would work out file paths based on the naming scheme, but it wasn't all underscores. So now instead of `$x = new Person()`, I'm doing stuff like `$x = new \Package\People\Person()` (contrived example). Just a bit harder to read sometimes, but I'm getting used to it. Netbeans 6.8 will auto-fill namespaces while you type though, so if I start typing `new Pers...` it figures it out. It will also create `use` statements at the top for you too, so I'm liking it.
zombat