24

I suggest the namespace WPSE for the code snippets we use in our answers.

Namespaces are a feature of PHP 5.3; WordPress core requires just PHP 5.2.4 currently. But even PHP 5.3 development has stopped already, PHP 5.4 is the stable version now, and 5.5 will be out soon.
PHP 5.2 was last updated in January 2011. Since then it didn’t even get security patches.

The WordPress ecosystem suffers from a copy&paste culture. We could use that to promote PHP upgrades. The more servers are updated, the sooner the core can update its requirements. I think we should discourage the use of insecure software.

The other benefit is cleaner code: function register_banner_widget() is much easier to read than function wpse_80202_register_banner_widget(). Namespaces are the better alternative to prefix everything.

Here is a sample plugin to illustrate how it could look:

<?php # -*- coding: utf-8 -*-
namespace WPSE;
/**
 * Plugin Name: Namespace WPSE
 * Description: Demo plugin for a common namespace on WordPress Stack Exchange.
 * Plugin URL:  http://wordpress.stackexchange.com
 */

\add_filter( 'admin_footer_text', __NAMESPACE__ . '\show_php_version' );

/**
 * Show the current PHP version in admin footer.
 *
 * @wp-hook admin_footer_text
 * @param   string $text Default text.
 * @return  string
 */
function show_php_version( $text )
{
    if ( ! \current_user_can( 'update_core' ) )
        return $text;

    return $text . ' PHP version ' . PHP_VERSION;
}

Your thoughts?

6
  • What about people that have their own namespace? For example my top level namespace is Rarst. How do we combine - WPSE\Rarst\... or Rarst\WPSE\... ?
    – Rarst
    Commented May 30, 2013 at 9:30
  • 1
    @Rarst I like your idea. I'd suggest to use WPSE\username\... as this would (in a perfect PSR-0 world) meant that we'd have a WPSE folder with subfolders that contains code snippets for each member. The other way 'round, we'd have all names as flat folder and then one unnecessary additional folder in it - which means less organisation and more clicks or keystrokes.
    – kaiser
    Commented May 30, 2013 at 9:34
  • 1
    love the idea but this should be a suggestion and nothing more, since sadly a large Percentage of servers (hosted servers, over 40%) still run php 5.2 according to w3techs.com/technologies/details/pl-php/5/all
    – Bainternet
    Commented May 30, 2013 at 10:56
  • @Rarst I would use user names in plugin headers only, but that’s up to you.
    – fuxia Mod
    Commented May 30, 2013 at 10:57
  • 2
    @Bainternet Of course, this is just a suggestion. The large number of outdated PHP installations is the point of my proposal: When our code breaks on the user’s site, we do not change the code, we ask to upgrade. Or, at least, that’s what I’ll do.
    – fuxia Mod
    Commented May 30, 2013 at 10:59
  • This might work if you included a link to a page with the remedies for broken code. - Did you copy and paste everything? - Ask your host to upgrade PHP. - This is how to remove the namespace stuff. Etc. Of course we can't label the link as "Instructions" because most programmers are men and men are not allowed to read instructions. :) Commented May 30, 2013 at 23:40

5 Answers 5

11

I like the idea, but I don't think it'll work.

The premise is that because an answer on the stack exchange site does not work for them (because they are using outdated php), they will upgrade. This assumes one of the following, with decreasing credibility

  • They can upgrade their PHP installation - most users run on hosted servers and are not able to do this
  • They can ask their host to update - Good luck.
  • They can switch host to one that supports php 5.3+ - Costly, and its easier/cheaper for the user to find a different answer elsewhere

My point is that people use WPSE, and WPSE is so popular, exactly because its the path of least resistance to the solution of their problem. (Yes this inevitably means we get a drive-by culture...) but if we starting posting answers which won't work on ~40% of servers then I think it'll damage WPSE's reputation has being a valuable resource for users.

Of course most developers will know to ensure their host is providing them with up-to-date software before registering with them - but the fact is WPSE has a large contingent of 'entry-level' users who would not.

This may work to 'raise awareness' - but I'm not sure how effective it'll be in pushing through changes to servers (it's a lot harder to get your host to do something it doesn't want to, than it is to just find a different answer).

As for a WPSE namespace, I like it - but I won't be using it my answers just yet :)

1
  • 3
    I have used lambda functions in my answers for a while now – no one complained. I think our user base is different than the average user. And they can always rewrite the code to make it work without the namespace. So we would force them to understand the solution thoroughly. :)
    – fuxia Mod
    Commented May 30, 2013 at 12:03
6

While I agree that most seasoned developers are running on a system that uses a better version of PHP, I disagree that using a namespace should become a standard convention. As you already pointed out, WordPress only requires PHP 5.2.4, and there are no signs of that changing in the immediate future.

Yes, many developers know how to refactor their code to not depend on PHP 5.3+. But many developers also come from the copy-and-paste culture - which means they're lazy. If their test server is running 5.3 (or 5.4 or 5.5), and the code works in the testbed, why rewrite it? I have had at least 20 people contact me via twitter or my website contact form because they've copied a function lambda into a plugin, tested it successfully on their own site, released it in a plugin, and had people complain about errors.

Until WordPress updates its minimum requirements, we should continue to support the lowest common denominator.

5

I like the idea-- it would save me from writing _wpse_XXXXXXX after every function-- but I don't think it should be a consideration until WordPress officially requires PHP 5.3. Once that happens, most of the objections become mute. If namespaces don't work, chances are WordPress doesn't work so it hardly matters. And once that happens I'd vote for the I wouldn't mind the WPSE\username\ pattern.

Another issues is going to be enforcement-- there won't be any. It will probably mean more "maintenance" edits to have any consistency to it.

As far as encouraging hosts to upgrade, just recently I had a major player in the business class bundled hosting/telecom/ISP industry refuse to update even to PHP 5.2.4. The best I got was "Yeah, we'll upgrade eventually but we don't know when."

4

I agree on the ugliness, but I think a better option for now and the foreseeable future is to use a small docblock.

  • It's code agnostic
  • It can be parsed by document engines
  • It's informative and somewhat standardized

Also the full url is a great reference since some posts have multiple answers and useful discussion/links. Plus if the code has an issue or changes, the answer can be revisited and adjusted at a later date, especially if the copy/paste has a viral nature.

 /**  
  * @category   Meta
  * @package    WordPress
  * @link       http://meta.wordpress.stackexchange.com/questions/2496
  * @author     toscho
  */

 function show_php_version( $text )
{
    if ( ! current_user_can( 'update_core' ) )
        return $text;

    return $text . ' PHP version ' . PHP_VERSION;
}
0

This is actually a terrible idea, and I would suggest that we should not only reject it, but disallow it and actively edit people's answers to NOT have it.

Despite how much you want to argue about PHP having moved on, the rest of the world clearly has not. PHP 5.2 is still actively used by more than 55% of WordPress installs.

enter image description here Oct 23, 2013

So the upshot of the suggestion is that you want to actively make code in answers not work for the majority of the user base. This is problem 1.

The result of this would eventually be people saying "this code broke my site" and other people responding with "take out the namespace declaration and it will work fine". This is problem 2.

As a result of problem 2, you are introducing code to the public which, after being "fixed" by well-meaning users who are just looking for solutions to their problems, have inadvertently introduced non-prefixed code into their installations, polluting their global namespace, and potentially introducing even more problems down the line from having all these hastily named functions in there. Let's call this problem 3.

So there's three problems with the idea. In my view, any one of these problems is enough to reject the concept entirely.

Namespaces are a great idea. But introducing them as some kind of active attempt to force the majority to upgrade, well, that isn't as great of an idea.

3
  • 3
    The goal of our site is not to provide copy & paste code. If the reader has to understand the code, she will be able to change it as needed. Whoever takes the code without understanding it is doing it wrong.
    – fuxia Mod
    Commented Jun 27, 2013 at 19:58
  • I agree, but that's no excuse for intentionally writing code fragments that will break sites when used.
    – Otto
    Commented Jun 28, 2013 at 12:55
  • 2
    I will write a follow-up in a few days, summarizing my experience from trying it out. Short story: nothing broke. :)
    – fuxia Mod
    Commented Jun 28, 2013 at 12:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .