-
Notifications
You must be signed in to change notification settings - Fork 226
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
Cannot redeclare requireDependencies() #175
Comments
Probably you are including twice the initialization script, how do you load the "autoload.php" script? |
I should be protected from that if everything is working right here. That's what autoload does. Check if class XY exists yet and load it if it doesn't. I have a lot of libraries included. Also e.g. all of laravel 5.1., mail service etc. That libraries work without errors. |
I think you are loading the library in the wrong way, or there was an error during the installation... I see the directory tree:
and I include the "autoload.php" in the root of the tree. Everything works fine for me. |
I will investigate a little on this and get back to you. As I see this we need more information to get to a valid conclusion. |
I extracted a minimal example that fails on the second "is_subclass_of".
Exception:
Tested on PHP 5.6.30 and 7. |
Braintree is not a class, but the root namespace. With those line of codes you are forcing the "autoloader" to load the file Braintree.php that does not contain any class definition. You will see the same error with these lines:
these lines do not cause the error:
It is an irrelevant issue since you never need to instantiate Braintree (that is not a class and therefore cannot be instantiated). However, if you want to solve this just add:
anywhere in Braintree.php. |
Omg! Is it so hard to wrap this "requireDependencies" in function_exists? Yes this Issue has 3 Sources:
Saving the user with rethinkdb in a totally unrelated area does the is_subclass check for every property |
I fixed the Issue in my code. For me there is no problem anymore. All others saving something with the name "Braintree" on it using rethinkdb will have this problem. But that's up to you, I have done enough. |
You can always open a "pull request." |
No, I'm done here. |
@RobIsHere @juongithub Thanks for investigating this issue. I'm concerned about your assertion that we are not following the PSR autoloader. The Braintree team will evaluate and see if we need to make an update to the SDK. |
Actually, it seems to me that psr specifications require a "root namespace" (i.e., a vendor name) but do not say nothing about the existence of a class with the same name. |
@crookedneighbor here are some pointers... http://www.php-fig.org/psr/psr-4/#overview http://www.php-fig.org/psr/psr-4/#specification Point 1 defines the supported structures: Note the absence of function. So the autoloader is probably not prepared for this. Breaking examples on the web: (I made the experience that following specifications to the point makes my software more reliable. So I personally would remove the file and search for another place to do the check. But thats definitely more work and only my opinion, a function_exists check should be good enough in practice) |
autoloader is not prepared to load a root namespace as a class :) |
*rofl :) |
@RobIsHere We took another look at this and want to make sure we understand your issue. It seems like Do we understand your problem? If so, we were thinking of changing the file to look like this: if (!function_exists('requireDependencies')) {
function requireDependencies() {
$requiredExtensions = ['xmlwriter', 'openssl', 'dom', 'hash', 'curl'];
foreach ($requiredExtensions AS $ext) {
if (!extension_loaded($ext)) {
throw new Braintree_Exception('The Braintree library requires the ' . $ext . ' extension.');
}
}
}
requireDependencies();
} If it's not too much trouble, could you try updating your |
My code looks like your code but with 4-spaces tabs :) |
@RobIsHere To clarify—did you make this change and did it work for you? |
Actually, the proposed solution is only a workaround, the root problem is that the autoloader thinks that the class Braintree is defined in the lib/Braintree.php file. The first time the class Braintree is required the autoloader loads the file lib/Braintree.php. The second time the class Braintree is required, the autoloader reload the lib/Braintree.php file since the class Braintree still does not exist, thus generating the error. There are several ways to solve this problem, but, in my opinion, a more coherent way should be preferred. For example, lib/Braintree.php could be renamed to lib/init.php, since Braintree is the root namespace and not a class. Furthermore, lib/Braintree.php is actually an initialization script. |
@juongithub The problem with that is that it's a breaking change for some people. If we rename the files, then this documentation will be wrong: require_once 'PATH_TO_BRAINTREE/lib/Braintree.php'; Do you know of a way to tell the autoloader about this difference? Or is the only solution to this problem a major version bump of the library? In any case: @RobIsHere does this workaround work for you? |
@RobIsHere I think you can change the autoload "psr-0" settings in composer.json from:
to:
this solves the problem in my local machine. |
Oh I missed that message. The upper solution is running and it works. Until the next update we will solve it, I'm sure! |
I don't want to do too much try and error because the problem only hit me on production. So I'm careful, I hope you understand that. That's how I would solve it: The responsibility to check the system configuration can go into the Configuration class. I would hook it somewhere early enough. Maybe that would be possible? |
I understand your point, in my opinion the best ways to solve the problem are (ordered from the most consistent to the least consistent):
However, I would avoid solution 4 since it does not prevent the autoloader to indefinitely re-load the file Braintree.php (i.e., the autoloader will reload the file Braintree.php every time the code requests the class Braintree); this does not happen with solution 3 since the class Braintree is defined the first time the file Braintree.php is loaded. |
…d-tests BTOPTMZ-164 remove sepa related tests
Hi!
I get the following error sometimes. Somehow this is loaded twice. I installed with composer and I'm using the default autoloader and no manual require statements at all. So I have no idea how this can happen.
Cannot redeclare requireDependencies() (previously declared in libs/braintree/braintree_php/lib/Braintree.php:17) in libs/braintree/braintree_php/lib/Braintree.php:17
The text was updated successfully, but these errors were encountered: