473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Working around a class name collision

2
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
2 2770
pbmods
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
Rolf
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

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

Similar topics

8
by: lawrence | last post by:
I'm a beginner with Javascript and especially cross-browser Javascript. I got this working in IE, but not in Netscape 7. It seems like, in Netscape, every time I click on a button, the focus shifts...
1
by: nntp | last post by:
It works in IE and Opera, but not in NS. Demo: http://javascript.internet.com/games/mouse-bomb.html Code: <!-- TWO STEPS TO INSTALL MOUSE BOMB: 1. Copy the coding into a new file, save as...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
1
by: Piotr Sawuk | last post by:
just a quick question out of curiosity: how to initialize const arrays? struct srat { long num; ulong den; srat(){} } struct collisions
7
by: Rich Grise | last post by:
OK, I don't know if this is Off-Topic for the group(s), because "QT" isn't "Pure C++", and Slackware is a distro, but those guys are sharp. :-) And I've crossposted to sci.electroncs.design because...
16
by: BartlebyScrivener | last post by:
I am a mere hobbyist. Spent several hours trying to make a class, because I think this is an occasion where I need one. But I can't make it work. This code "works" (only because of the global c,...
8
by: Noob | last post by:
Hello, Consider the following scenario. 'A' produces data which are sent in "packets" to 'B'. Each "packet" carries a sequence number, so that 'B' can insert the packet in the "right place" in...
12
by: Aaron Gray | last post by:
In FF you can define the following ... Element.prototype.getClass = function() { return this.className; /* or whatever */ } Element.prototype.setClass = function(c) { this.className = c; } ...
3
by: PrabodhanP | last post by:
I have CSS based mouseover scrolling for divContent embeded in my webpage.It works fine in IE,but not working in mozilla-FF. It is located at the location.....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.