views:

46

answers:

3

I'm trying to include some functions from phpBB board to my application like a single login. But if i include the common.php it says "Cannot redeclare class user", because my application already has a class user. Is there a way to avoid this ? I tried a little with namespaces, but I never worked with them.

So I need a way to include 2 classes with the same name.

A: 

Namespaces are the only way.

Bart van Heukelom
can you push me in the right direction how to work with those namespaces for this problem?
XzenTorXz
you need PHP 5.3 for the namespace support. Generally, if you use it, you will have to make a lot of changes in the code - namespaces must be defined everywhere. It's easier just to rename your class to, let's say, 'myuser'
Silver Light
@Silver Light: You don't have to use namespaces everywhere. You just need to put one user class in a namespace and update references to that class in the files where it's used. The rest you can leave alone. That said, renaming the class probably is the easier solution here.
Bart van Heukelom
well renaming my classes would be a pain in the ass :-/ because I use a lot of dynamic calls, wich my IDE can't handle in refactoring. I will continue search for a solution for a while.
XzenTorXz
@Bart van Heukelom, thats what I meant, just having trouble to make my thoughts clear :)
Silver Light
@XzenTorXz: I think searching will be a waste of time, since there's no way around renaming the class one way or another (adding a namespace is also a rename when you use dynamic calls)
Bart van Heukelom
A: 

Just rename your class. not a big deal

Col. Shrapnel
+1  A: 

As mentioned in the other answers to this question, there is no way of getting around this without renaming your class or going through the fuss of getting namespaces working (e.g. ensuring you have the right PHP version).

A good general habit is to namespace your class names by habit. If you look at, for instance, Zend packages, you will see that every class name is prefixed by Zend_, e.g. Zend_Mail, Zend_Mime, Zend_Mime_Part. This means that (a) there is a logical relationship between classes that have related functions and (b) that classes are unlikely to conflict with those made by you or by another framework. This pattern is followed by other projects, such as PEAR.

This requirement is made obsolete by the introduction of PHP 5.3. However, 5.3 is not yet widely adopted, especially by shared hosting providers, and this solution may well be a good one for your current situation.

lonesomeday