472,139 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

Working around a class name collision


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 {
  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.       }
  10. }
And then inside the blog page

Expand|Select|Wrap|Line Numbers
  1. <?php
  3. // insert wrapper here
  5.       require_once ‘class.Wrapper.php';
  7.       $w = new Wrapper(7900);
  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.

Aug 28 '07 #1
2 2717
5,821 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:


Hope this helps people out there.

Aug 28 '07 #3

Post your reply

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

Similar topics

1 post views Thread by nntp | last post: by
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 Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.