Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Fix problem with string callables failing in PHP 5.6. #206

Merged
merged 2 commits into from
Nov 27, 2017

Conversation

demiankatz
Copy link
Contributor

ServiceManager v2 supported configuring factories to callable strings of the form 'Class::Method', as a concise alternative to ['Class', 'Method'].

ServiceManager v3 continues to support this for PHP 7, but for some reason, the feature breaks in PHP 5.6.

This pull request includes a possible fix as well as a test that demonstrates the problem; without my proposed code changes to the ServiceManager, the test dies with a fatal error in PHP 5.6, but passes in PHP 7.

If there is a more elegant solution, I would welcome feedback. If this support is being dropped intentionally, we should at least find a way to fail more gracefully.

@demiankatz
Copy link
Contributor Author

@Xerkus, I received a notification that you had left a comment here, but I no longer see it. Does that mean you were able to reproduce the problem successfully?

@Xerkus
Copy link
Member

Xerkus commented Nov 17, 2017

Yes, I made wrong test case at first https://3v4l.org/h8MEs

@demiankatz
Copy link
Contributor Author

Thanks!

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this feature is adapting code to work under PHP 5.6, we need to do version testing, so we can remove the changes once we drop PHP 5 support.

@@ -668,6 +668,10 @@ private function getFactory($name)
if ($lazyLoaded) {
$this->factories[$name] = $factory;
}
// PHP 5.6 fails on 'class::method' callables unless we explode them:
if (is_string($factory) && strpos($factory, '::') !== false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is PHP 5.6-specific, let's add a check for being run under PHP 5.6, so that we can remove this once we drop PHP 5 support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I've made the change using the PHP_MAJOR_VERSION constant; please let me know if you have a different preferred way of doing version checking in the code.

return new stdClass();
}

public function testStaticCallable()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a more descriptive name, something like testMapsStaticCallableMethodsAsArrayCallablesUnderPHP56. Also, limit this test to PHP 5.6.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is not actually testing anything PHP 5.6-specific. It is simply testing whether a service manager can accept a callable string of the format class::method to use a static method as a factory. As such, I have given the method a more descriptive name but have not made it a version-specific test; this is a behavior of the service manager that some people depend on, so I think it's worth testing for future regressions in case something different causes this behavior to break. Of course, I'll happily change this if you still feel it is necessary -- but I wrote the test with the intention of exercising a generic behavior, not trying to capture the implementation details of my specific fix.

Copy link
Contributor Author

@demiankatz demiankatz Nov 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also occurs to me that we could, if you think it worthwhile, add a complementary testFactoryMayBeStaticMethodDescribedByArray test to demonstrate that a configuration using an array instead of a delimited string does exactly the same thing. I'll happily add this if you think it's useful, but I'm holding off for now in the interest of avoiding adding unnecessary noise to the PR.

@weierophinney weierophinney merged commit 8d84c79 into zendframework:master Nov 27, 2017
weierophinney added a commit that referenced this pull request Nov 27, 2017
Fix problem with string callables failing in PHP 5.6.
weierophinney added a commit that referenced this pull request Nov 27, 2017
weierophinney added a commit that referenced this pull request Nov 27, 2017
weierophinney added a commit that referenced this pull request Nov 27, 2017
@weierophinney
Copy link
Member

Thanks, @demiankatz

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

Successfully merging this pull request may close these issues.

3 participants