467,083 Members | 1,259 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,083 developers. It's quick & easy.

Working around a class name collision

Hello-

I’m integrating blogging software (www.b2evolution.net) into my app and have run into a namespace-collision problem. In my code, I’ve defined a User class, and so have the b2evolution writers, so there’s a collision.

I tried simply renaming their User class to something else throughout their code (e.g., BlogUser) but as a first pass it didn’t work since the “User” name is hardcoded in many places in their code where the class is instantiated on the fly.

None of my classes need to be used by the blog, so if I could hide their class definitions, all would work. I tried, for example making a wrapper class so my User class was included inside it, but it’s still visible outside (bad):

Expand|Select|Wrap|Line Numbers
  1. class Wrapper {
  2.  
  3.       public function __construct($page_id) 
  4.     {
  5.     // include my “User” class here, inside the constructor, hopefully the class definition it won’t be visible outside this class
  6.              include  '/class.User.php';
  7.     //do init stuff here
  8.       }
  9.  
  10. }
And then inside the blog page

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // insert wrapper here
  4.  
  5.       require_once ‘class.Wrapper.php';
  6.  
  7.       $w = new Wrapper(7900);
  8.  
  9.     print_r(new User(1));
  10. ?>
 Still outputs my User class (bad). Thus it appears class definitions are always global, regardless of where they get included from.


What’s the recommended way of working around this? For example, is it possible to change the name of a class after definition (if so I could just define my class as usual, then rename it from User to MyUser, let b2evolution define User afterward, and be done).

Thanks in advance.

-Rolf
Aug 28 '07 #1
  • viewed: 2535
Share:
2 Replies
pbmods
Expert 4TB
Heya, Rolf. Welcome to TSDN!

PHP does not support namespaces, so class names must be unique.

Your only option is to rename one of the two classes.
Aug 28 '07 #2
Hi all-

I figured out a workaround, the key is to create a wrapper class that creates a Runkit_Sandbox that in turn requires my own classes. In that way my classes won't be visible to the external ones, and vice versa. Runkit has to be enabled as an extension and is packaged with PHP5.1+ - more info available here:

http://us.php.net/manual/en/runkit.sandbox.php

Hope this helps people out there.

-Rolf
Aug 28 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

166 posts views Thread by Graham | last post: by
1 post views Thread by Piotr Sawuk | last post: by
16 posts views Thread by BartlebyScrivener | last post: by
8 posts views Thread by Noob | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.