Yesterday, a member of the core team asked me for help. He didn't remembered
the naming convention for bundle extensions. The convention is simple enough
as this is basically the name of the bundle with Extension
as a suffix
instead of Bundle
. But that didn't work for him. Frankly, we were stuck. I
had no idea why it wouldn't work for him as it definitely works for me. But a
few minutes later, he found the problem. He had a typo in the namespace
declaration:
// file is Foo/Bar/Baz.php
namespace Foo/Bab;
// notice that the namespace is Foo/Bab
// but the path is Foo/Bar!
class Baz
{
// ...
}
No way I would have been able to find the problem as I had no access to his code. This is really frustrating as he does not work for the person you are trying to help, but you know that this should definitely work. At some point, you start yelling at the framework, and eventually you give up because the damn thing is too "complex". I had the exact same problem more than once in the past and I bet that most of you had it too. And if you don't give up, you can spend hours before spotting the problem... and start yelling at you!
Even if the problem has nothing to do with the framework, is it possible to enhance the developer experience here? Can Symfony2 help the developer avoiding this error in the first place? Every time someone reports a problem like this one, we should think how we can change the framework for the better.
After thinking a bit about this specific issue last night, it turns out that we can easily improve the situation and save hours of frustration all around the world.
Whenever the standard Symfony2 autoloader finds a file where the class to autoload should be defined, we can easily check that the class is effectively declared in the file we have just included. If that's not the case, there is something wrong:
if (!class_exists($class, false) && !interface_exists($class, false)) {
throw new \Exception(sprintf(
'The autoloader expected class "%s" to be defined in file "%s".
You probably have a typo in the namespace or the class name.',
$class, $file));
}
The actual patch is a bit more complex as it should not slow down your application in production, but you get the idea.
This enhancement will be available in Symfony SE beta2.
This is superb. Thinks like this are really important since most of the deadlines are broken because of situations like this.
Great job.
PHP Manual says, that namespace separator character is "", not "/" as in your code. Do I miss something?
Those kind of things are what make Symfony not just a great framework (the greatest, ;)) but a new way of thinking about code.
And if every member of the community asks him/herself "how we can change the framework for the better" when hitting these types of "pain points", we'll all save each other thousands of hours debugging in the future.
That's the power of having a community that's focused on the developer experience.
Fabien, please stop making Sf2 easier immediately.
How will I make my customers pay while it becomes so easy !
I do really enjoy Symfony. I appreciate all the effort made especially by Fabien, you are one of the most unselfish developers I ever heard about / met. When community talk you listen, and that is invaluable.
The namespace should have the slashes going the other way. That is, you should do Foo\Bab not Foo/Bab.
Typo: "He didn't remembered the naming convention..." should be "He didn't remember the naming convention..."
I think you 'fix' is cute, but working on better documentation for many parts of Symfony2 is more important. As someone who develops in Symfony2, if I run into a problem when adding a class or bundle, the first thing I always check is namespaces. Always.
Hi Fabien,
First great great work. I've been experimenting with symfony 2 for a week now, overcoming some problems with tampering with the codes, updating components and all.
however, yesterday i came across validation with yaml and it seemed it does not work. pray tell me, master, is it in place yet? or i should look even harder into my code?
god speed!
How about implementing a levenshtein based suggestion for that kind of mistakes?
Hey, great framework i started studying it 3 days ago, but there's something i find hard to understand and it's the Security chapter (the ACL thingy), too many new conceps for me... could you add some kind of easy step by step examples, i think many developers like myself might be in my same situation (spending hours trying to figure out how the security thing works... lol)
I've got a similar problem yesterday. As it turned out, it was a kind of cache issue.
Idea: Create some sort of command that lists all the files/classes "recognized" by the framework. I.e: bundles, DI extensions, etc.