4

Just a quick question on your preference of using PHP namespaces or class prefixing.

<?php
// ----- namespace -----
use My\Namespace;
$object = new Namespace\Object;

// ----- prefix with PR -----
$object = new PF_Object; //or
$object = new PFObject;

Which do developers prefer? I know why the use of namespaces can bring great advantage to applications, but also can quite a hindrance in PHP in my own opinion.

Thanks!

Chris
  • 83
  • 9
  • It's too bloated question. Probably that developers who came from namespaced-OOP-programming world like `Python` or `Java` will prefer 1st more, others would probably choose 2nd... It all depends, you know... As many people, as many opinions. – Nemoden May 30 '11 at 13:30
  • Well everyone who used a real OOP language is likely to avoid PHPs namespace syntax. But apart from that personal preference, it's more a question of if you're on one of the [5% of servers](http://phpadvent.org/2010/usage-statistics-by-ilia-alshanetsky) with php 5.3 support. – mario May 30 '11 at 13:33
  • @mario 5% using php 5.3, thats a shocking statistic :O – Chris May 30 '11 at 13:34
  • Albeit that was end of last year. It might very well be hovering above 10 by now. – mario May 30 '11 at 13:35

4 Answers4

2

Combine use with an alias:

use My\Namespace\Foo as Foo;

$object = new Foo;

It's also useful like so:

namespace My\Debug\Stuff;
use My\Production\Stuff\Foo as BaseFoo;

class Foo extends BaseFoo {}
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
2

Why would you consider namespaces a hindrance?

class prefixing seemed to me like a sort of a hack to implement 'in code' a mecanism to implement namespaces.

Newer code now has the option to use a native built-in way of handling namespaces. This is a much cleaner approach, in my very humble opinion.

Consider this popular yet eye-opening example that allows legacy code to use namespace:

// Using native namespace features to shorten class prefixes

<?php
use Sabre_DAV_Auth_Backend_PDO as AuthBackend;
use Zend_Controller_Action_Helper_AutoComplete_Abstract as AutoComplete;

$backend = new AuthBackend();
?>
Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
  • I just hate the character used for namespace separation in PHP, makes it feel like your referencing location on a windows system. You also have made a great point to in handling namespace classes, thank you! – Chris May 30 '11 at 13:42
0

Clearly, there's no need to use the underscore character to fake namespaces any more, so I suppose the question boils down to "is it a good idea to use underscore characters in class names".

IMO, the answer is no, firstly because it looks ugly (IMO) and secondly because some older class loaders could get confused by a class name that includes an underscore.

Robin
  • 4,242
  • 1
  • 20
  • 20
0

well if you are running PHP 5.2.X you only have the option 2

but if you are running PHP 5.3.Xyou could use both.

In my case running PHP 5.3.X I would use the feature that the new version of the language offers. To make an analogy is likely be running JAVA 1.6 and don't use generics (or sort of)

Gabriel Sosa
  • 7,897
  • 4
  • 38
  • 48