Implemented Allow namespace use in listeners

Robbo

Well-known member
Currently namespaces can't be used for extending a class, as in the following...

PHP:
<?php namespace Robbo\DataWriter;

class User extends XFCP_User {
  // stuff
}

Because when you extend in the listener with...

PHP:
$extend[] = 'Robbo\DataWriter\User';

It is trying to eval
PHP:
class Robbo\DataWriter\User extends XFCP_Robbo\DataWriter\User {}
which obviously won't work.

So if you make the following changes to XenForo_Application, line 416...

PHP:
                foreach ($extend AS $dynamicClass)
                {
                    if (strpos($dynamicClass, '\\') !== false)
                    {
                        $namespace = substr($dynamicClass, 0, strrpos($dynamicClass, '\\'));
                        $proxyClass = 'XFCP_' . substr(strrchr($dynamicClass, '\\'), 1);
                    }
                    else
                    {
                        $namespace = false;
                        $proxyClass = 'XFCP_' . $dynamicClass;
                    }

                    eval(($namespace ? 'namespace ' . $namespace . '; ' : '') . 'class ' . $proxyClass . ' extends \\' . $createClass . ' {}');
                    XenForo_Application::autoload($dynamicClass);
                    $createClass = $dynamicClass;
                }

Tested with a couple classes and they all seem to inherit fine, full namespace is just used on the extend and if a namespace exists the eval just starts with that. I haven't however tested the fake base stuff above these lines because I have never needed to use it so not sure how it should be modified.

Just need to know if this can be in 1.2 so I can start coding my listeners like this or make a workaround.
 
Upvote 0
This suggestion has been implemented. Votes are no longer accepted.
Why would you use namespaces when XenForo itself doesn't? Effectively causing your minimum PHP version to be higher than XenForo's.
 
I don't think XenForo should have extra code to support something they themselves don't utilize. And until XenForo ups its minimum required version, I don't feel its necessary to support name spaced classes.
 
So if they don't utilize certain template and code hooks in the ES or Resource Manager they should take those out too right? Just a bunch of extra code to support things they don't use themselves :rolleyes: Oh wait, those are for addons.. which should be able to use them how they want?

They're on ZF1 so they're not going to up the minimum version anytime soon. PHP 5.3 itself is EOL, you should be able to make full use of namespaces if you want to.
 
You misunderstand my statement. Namespaces are a programming system they don't utilize themselves, however, they do utilize hooks and code event listeners. I don't see a real use in XenForo implementing, testing, and supporting something that they most likely won't utilize until XenForo 2.0 (most likely time to upgrade Zend and/or remove it to not break backwards compatibility).
 
My point is this request is for addons to make use of, and addons should be able to make use of any PHP syntax they want, especially now that namespaces exist in all supported versions of PHP. Just because they don't use it themselves doesn't mean it shouldn't be supported.

I get it would cause incompatibility with users running XF on 5.2, but that's not my problem. I as a developer should be able to choose a greater minimum required version if I want. I personally would like to namespace my addons, the majority of which are private to me anyways so compatibility with other setups isn't my concern.
 
I never used namespaces for proxy classes, because there's IMO no real benefit... You'll never use the classname of the proxy class in other classes, except in the listener declaration (or however this is being called) , but why not?:D

XenForo shouldn't stop anybody to use 5.3, 5.4 or even 5.5 features, just because xenforo requires 5.2. I don't want to live in the past and stick with 5.2 code (

Just think of all the new great features in php 5.3 (namespaces, closures) ,5.4 (traits) and now all the great new features in 5.5:love::love: oooooooooooh, how i fall in love with some of them like the usage of list inside foreach

As long as it's just a minimal change in xf core and doesn't bring new BC problems, why not?(just like the small change in 1.1.4?, to support namespaces in the xf autoloader)
 
Last edited:
Get with the times? I'm fairly sure that the 'times' still have a majority of users on 5.2. I'm sorry that alienating a majority of users is not in my end goal.
 
This functionality doesn't break BC though, I don't see how this is alienating in the slightest. You're still welcome to pretend namespaces don't exist just like you did a week ago, if I want to "alienate"' 5.2 users I should have that choice as an addon developer.
 
Get with the times? I'm fairly sure that the 'times' still have a majority of users on 5.2. I'm sorry that alienating a majority of users is not in my end goal.
Haha what? I have issues with my add-ons with PHP 5.2. They do not work. The amount of time it took me to try find what stupid **** PHP was doing was more than it took for the customer (a single customer on an OLD version of PHP) to just see about updating his PHP version. It will literally be cheaper and quicker for me to help people upgrade PHP 5.2 or choose a host that isn't crap.

The suggested code would actually break XF with PHP 5.2 so that would break BC... It's possible to do it with out breaking BC as far as I can tell though, so I've done that for the next beta.
What in it doesn't work with 5.2? Just curious. And big thanks for adding this.
 
Top Bottom