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
- class Wrapper {
- public function __construct($page_id)
- {
- // include my “User” class here, inside the constructor, hopefully the class definition it won’t be visible outside this class
- include '/class.User.php';
- //do init stuff here
- }
- }
Expand|Select|Wrap|Line Numbers
- <?php
- // insert wrapper here
- require_once ‘class.Wrapper.php';
- $w = new Wrapper(7900);
- print_r(new User(1));
- ?>
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