Please improve function and global var namespacing

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The next feature release of phpBB 3 will be 3.2/Rhea followed by 3.3.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.1. If you need support for phpBB 3.1 please visit the 3.1.x Support Forum on phpbb.com.

If you have questions regarding writing extensions please post in Extension Writers Discussion to receive proper guidance from our staff and community.
Jhong
Registered User
Posts: 50
Joined: Tue Dec 26, 2006 3:28 pm

Please improve function and global var namespacing

Post by Jhong » Tue Aug 31, 2010 7:38 am

I understand that 3.1 will be much more OOP, but I still think this is worth mentioning.

One of my long-term bug-bears with phpBB has been poor function and global variable naming. Makes my life very difficult:

Right now, these functions cause me trouble:
validate_username
make_clickable

These global variables are problems too:
$user
$template
(others...)

phpBB2 had similar problems (albeit with different functions and variables)

Specifically, these common names make integrating phpBB with third-party packages difficult.

In my case, it is WordPress. Yes, WordPress is to blame too, but since they have a mature plugin ecosystem and a huge number of plugins, it is impossible now for them to rename their functions and global variables.

However, in phpBB's case, with the impending move to a new modding API, this is an excellent time to review all functions and variables in the global namespace and give them better names. Even just prefixing phpbb_ would be fine by me.

e.g....

$db -> $phpbb_db
$user -> $phpbb_user
$template -> $phpbb_template
make_clickable -> make_forumpost_clickable
validate_username -> validate_phpbb_username

Undoubtedly new code will get added to 3.1 that will result in new conflicts with other packages as well. It would be nice to have a process in place to ensure this doesn't happen.

I think this will help from a security perspective too, as users might naively integrate phpbb with other packages on their site without noticing insidious modification of global variables.

J

User avatar
ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: Please improve function and global var namespacing

Post by ToonArmy » Tue Aug 31, 2010 12:53 pm

Jhong wrote:make_clickable -> make_forumpost_clickable
validate_username -> validate_phpbb_username
Prefix the functions with 'phpbb_' rather than renaming them to something that again may conflict with something else.
Jhong wrote:In my case, it is WordPress. Yes, WordPress is to blame too, but since they have a mature plugin ecosystem and a huge number of plugins, it is impossible now for them to rename their functions and global variables.
We have a huge number of MODs that use our functions some of these may still work with 3.1 without updating, the large addon type ones for example. An effort needs to be made from all vendors to namespace functions, either using PHP 5.3 namespaces or naming conventions.
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image

Jhong
Registered User
Posts: 50
Joined: Tue Dec 26, 2006 3:28 pm

Re: Please improve function and global var namespacing

Post by Jhong » Tue Aug 31, 2010 1:48 pm

I agree -- yes, it is all packages' job to namespace properly.

How about this: a compatibility layer that is off by default?

The compatibility layer need only be an include file that passes old functions to new ones, and creates references to global variables using the old names.

User avatar
EXreaction
Registered User
Posts: 1555
Joined: Sat Sep 10, 2005 2:15 am

Re: Please improve function and global var namespacing

Post by EXreaction » Tue Aug 31, 2010 6:14 pm

This was part of the plan for 3.2 originally, but when the development plan changed it was decided to scrap it as it would require too many changes to work properly and would break mod compatibility, something we did not want for the 3.x line.

4.x should be namespaced properly, there shouldn't be any global variables like $user used, and everything should have unique non-conflicting names (prefixed with phpbb_).

User avatar
bantu
3.0 Release Manager
3.0 Release Manager
Posts: 557
Joined: Thu Sep 07, 2006 11:22 am
Location: Karlsruhe, Germany
Contact:

Re: Please improve function and global var namespacing

Post by bantu » Tue Aug 31, 2010 7:05 pm

EXreaction wrote:4.x should be namespaced properly, there shouldn't be any global variables like $user used, and everything should have unique non-conflicting names (prefixed with phpbb_).
4.x actually requires PHP 5.3 which has namespacing support, thus classes and function do _not_ have to be prefixed with phpbb_.

Edit: Since this does not only affect function and class names but also global variable names ... I don't see a way of having this in the 3.x branch ... should have been a lesson learned from 2.y.

User avatar
EXreaction
Registered User
Posts: 1555
Joined: Sat Sep 10, 2005 2:15 am

Re: Please improve function and global var namespacing

Post by EXreaction » Tue Aug 31, 2010 7:43 pm

True, it doesn't really matter with namespaces, but I thought the idea was to use phpbb_ for most things anyways (unless that was just for new things added for 3.x).

User avatar
ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: Please improve function and global var namespacing

Post by ToonArmy » Tue Aug 31, 2010 11:15 pm

bantu wrote:
EXreaction wrote:4.x should be namespaced properly, there shouldn't be any global variables like $user used, and everything should have unique non-conflicting names (prefixed with phpbb_).
4.x actually requires PHP 5.3 which has namespacing support, thus classes and function do _not_ have to be prefixed with phpbb_.

Edit: Since this does not only affect function and class names but also global variable names ... I don't see a way of having this in the 3.x branch ... should have been a lesson learned from 2.y.
No reason why we can't namespace some of the global functions and classes over time as those are the real kickers, global variables can be worked around.
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image

Jhong
Registered User
Posts: 50
Joined: Tue Dec 26, 2006 3:28 pm

Re: Please improve function and global var namespacing

Post by Jhong » Wed Sep 01, 2010 1:46 am

Yes, if we could get the low-hanging fruit, that would be great --- just renaming make_clickable and validate_username for now would solve the immediate problem with WordPress.

Even if in the future we use PHP 5.3 namespaces (in all their weird backslash glory), I assume it is only going to help if other packages we wish to integrate to also use namespaces?
Last edited by Jhong on Wed Sep 01, 2010 5:43 am, edited 1 time in total.

User avatar
igorw
Registered User
Posts: 500
Joined: Thu Jan 04, 2007 11:47 pm

Re: Please improve function and global var namespacing

Post by igorw » Wed Sep 01, 2010 5:12 am

I actually quite like the idea of a "compatibility layer" that can be disabled by hooks/bridges. Basically a wrapper around the renamed functions (make_clickable making a call to phpbb_make_clickable). I would however enable this layer by default. There could either be one single constant that decides whether the compatibility functions are to be loaded, or (more advanced) a class that manages these and provides an API for enabling certain functions.

The difficulty with changing names is also merging bugfixes later on, but in this case I feel it may be worth the change. If we can fix issues for other softwares than WordPress, this would be great.

Jhong
Registered User
Posts: 50
Joined: Tue Dec 26, 2006 3:28 pm

Re: Please improve function and global var namespacing

Post by Jhong » Wed Sep 01, 2010 5:54 am

In that case perhaps call it a deprecation layer rather than a compatibility layer.

In WordPress they have a deprecation layer -- there are also hooks to inform authors that they are using deprecated functions (or include files): http://codex.wordpress.org/WordPress_De ... tions_Hook

That said, I think that is probably over-egging something that should be quite simple -- covering it in modding documentation would probably be as effective as adding code hooks.

If function definitions in the layer were wrapped with if(!function_exists('xxx')) then it could run itself. (However I guess MODs would need to be able to delay its inclusion until after an integrated package has been added to the namespace).

In terms of identifying namespace collisions, how could that be achieved? I guess first step is to identify major packages we are interested in -- e.g. Joomla, Drupal, MediaWiki. WordPress, etc., then parsing their code or documentation for function declarations. Global variables are more difficult though. You could do a regex search across all their files for "global XXX" and "$GLOBALS['xxx']", but it still wouldn't catch everything.

Post Reply