Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Namespace+Autoloading compatibility #120

Merged
merged 1 commit into from Jul 27, 2013
Merged

Added Namespace+Autoloading compatibility #120

merged 1 commit into from Jul 27, 2013

Conversation

WietseWind
Copy link
Contributor

Little piece of PHP to make Mobile_Detect auto-loadable in PSR-0 compatible PHP autoloaders like the Symfony Universal ClassLoader by Fabien Potencier. Since PSR-0 handles an underscore in classnames (on the filesystem) as a slash, "Mobile_Detect.php" autoloaders will try to convert the classname and path to "Mobile\Detect.php". This script will ensure autoloading with:

  • Namespace: Detection
  • Classname: MobileDetect
  • Namespased: \Detection\MobileDetect
  • Autoload path: ./namespaced
  • Converted path: ./namespaced/Detection/MobileDetect.php

@serbanghita
Copy link
Owner

@WietseWind thanks for pointing it out. This was mentioned before in #80
My only concern is that many people using the class are still using PHP version < 5.3.

Not sure how to make the transition yet.

@ghost ghost assigned serbanghita Jul 10, 2013
@WietseWind
Copy link
Contributor Author

@serbanghita The great thing about my suggested fix is it's on top of de current, unnamespaced, un-autoloadable class; it just namespaces and extends the current class. Without any changes people can choose to use the (unchanged) unnamespaced version, or the namespaced, autoloadable one.

@serbanghita
Copy link
Owner

@WietseWind I planned to respond earlier but god distracted. So, I got it now, I was missing the context. Is there anyway you can provide a real world example. I'm interested in explaining the new folder to people. How this changes the integration of the script into Symfony. Thanks!

@WietseWind
Copy link
Contributor Author

Hi @serbanghita,

The Symfony Classloader makes auto-loading classes very easy;

  1. Include the autoloader, and define the classes + source-location (stored and coded in the PSR-0 standard)
    schermafbeelding 2013-07-11 om 12 54 24
  2. To use the class, you just need to "use" the namespace+class. Now you can just create a new Class, no more inluding required. When the class isn't used, it isn't (auto) loaded so no memory-overhad.
    schermafbeelding 2013-07-11 om 12 54 51

@vlakoff
Copy link
Contributor

vlakoff commented Jul 22, 2013

You may want to use slashes instead of DIRECTORY_SEPARATOR, they work in windows too.

@serbanghita
Copy link
Owner

@vlakoff I don't see no reason to replace DIRECTORY_SEPARATOR (on nix is / on win is \).
PS: http://stackoverflow.com/questions/625332/is-using-the-directory-separator-constant-neccessary

@vlakoff
Copy link
Contributor

vlakoff commented Jul 22, 2013

I just think it is a true pain to read :-p (which leads to worse maintainability)

This article has made a perfect summary. That being said, we don't want to go off-topic ;-)

@DaniSancas
Copy link

Good change! I started using your code inside Symfony2, and it's working!

Greetings!

@WietseWind
Copy link
Contributor Author

@DaniSancas Great :)

serbanghita added a commit that referenced this pull request Jul 27, 2013
Added Namespace+Autoloading compatibility
@serbanghita serbanghita merged commit 7a04987 into serbanghita:master Jul 27, 2013
@serbanghita
Copy link
Owner

I'll tag this in the 2.6.4 release. Thanks a lot!

@WietseWind
Copy link
Contributor Author

Great, thanks!

@vlakoff
Copy link
Contributor

vlakoff commented Jul 28, 2013

Another small thing, since PHP 5.3 is required, you are sure the __DIR__ constant is available ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants