Issues with global varables and nested functions
SteveALee opened this issue · comments
I'm liking kahlan after returning to PHP after a break having been steeped in JavaScript land. The runner and BDD style mean errors are presented nicely, especially the verbose reporter.
I hit 2 strange effects on my code causing me to rewrite so the code still worked. My code is more functional style that OO.
I think the pobolems must be due to closures introduced by the tests. I had the same issues with peridot.
-
global vars stop being global. Ie a variable declared at file scope and then referenced from a function with global fail. It seems file scope is no longer global when running the tests. The solution is to declare the file scope variable as global too. (in case you wonder I only have global vars as static vars in a function cannot be initialised with a dynamic exression :( )
-
Nested functions give a weird "previously declared" error. This is truely puzzling and I had to move the function definitons to file scope and then all is well.
Hi, some examples would help to figure out if it can be considered as a bug or not.
Oh, I added some - but I guess I forgot to save d'oh - will add later.
@jails So one thing is not clear to me- should I explicitly require() the code I'm testing into the spec file? You learn example doesn;t but I think that might be due to some autoload magic? I'm new to PHP so am not using autoload and I find I have to require the file despite mirroring the directory structure. This could well be related to the strange issues I see.
I'm thinking you have made some assumptions that I have not met as the runner shows this.
src directory :
spec directory : /home/wsl/mu/payment-tracking/spec
Indeed Kahlan relies on the Composer autoloader which is a dependency manager for PHP so the require()
statement should be used only in some very specific situations. If you're new to PHP I would advice to clone a simple project to understand how to bootstrap something alongside Kahlan.
Since it's more questions that issues, I'm closing this since this place is dedicated trace Kahlan's bugs.
@jails The bug is you have not documented something critical.. You do NOT have to use composer package manager for dependencies. Plus I'm not going to create packages for my code ! I'm actually not new - just returning since a while before composer. I'll look at your example to figure out what you have not documented. thanks.
Plus autoload seems to be all about classes - and I'm writing functional style. oh well back to PHPUnit.
@jails Thanks for that simple Text example. It helped me understand that the idiomatic PHP way to do modules is now a namespaced Class with autoload. While having a Class with only static members seems rather like taking a treadmill onto a bus to get some exercise, it's workable. Especially now the JavaScript finally has proper ECM modules. Given that The bugs I reported are there but never usually be experienced.
PS, I apologise, the requirement to use psr4 autoload IS documented. It's under dependencies, but not the getting started section. Cheers