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

ServerRequest::fromGlobals() should use new static() #212

Closed
bkdotcom opened this issue Aug 27, 2018 · 3 comments
Closed

ServerRequest::fromGlobals() should use new static() #212

bkdotcom opened this issue Aug 27, 2018 · 3 comments

Comments

@bkdotcom
Copy link

Currently:

class MyExtendedRequest extends \GuzzleHttp\Psr7\ServerRequest
{
}

// This creates a ServerRequest object, not a MyExtendedRequest object :(
$request = MyExtendedRequest::fromGlobals(); 

fromGlobals calls new ServerRequest(...). it should call new static(...)
Workaround is for extended class to extend (recreate) the fromGlobals method (but with static)

@bkdotcom bkdotcom changed the title ServerRequest::fromGlobals() should use static keywork ServerRequest::fromGlobals() should use static keyword Aug 27, 2018
bkdotcom added a commit to bkdotcom/psr7 that referenced this issue Aug 27, 2018
fromGlobals() : use static keyword
@bkdotcom bkdotcom changed the title ServerRequest::fromGlobals() should use static keyword ServerRequest::fromGlobals() should use new static() Aug 27, 2018
@sagikazarmark
Copy link
Member

This has been rejected once. Please see #158 and #159 for explanations.

@Nyholm Nyholm closed this as completed Aug 27, 2018
@bkdotcom
Copy link
Author

bkdotcom commented Aug 28, 2018

Oops... my quick search for existing issues came up empty...

To Quote @kael-shipman comment on #158

The second thing I did was try to instantiate my new subclass using the fromGlobals method, which, by all counts, should have worked as expected. To my surprise, it didn't, and I had to debug it, eventually discovering what I thought was a bug and reporting it. This wasted my time and yours, and obvious @ilyar's, too, when he found it and reported it, and will probably waste other people's time as more people run into this and try to report it (and yours along with it as you close the repeat bug reports and point people to this discussion).

I'm just wanting a few Slim-like methods, isXhr(), getCookieParam(), getQueryParam, etc

The arguments against LSB (PSR7 doesn't define constructor / ServerRequest is a value object) frankly don't make sense to me. But I respect your judgement.

Suggestion: add a // this is not a bug comment to the code

@sagikazarmark
Copy link
Member

@bkdotcom What about plain old functions?

function isXhr(ServerRequestInterface $request)
{
    return true;
}

IMO that fits better your use case since it would work with any implementation and you don't have to check or make sure that the request is always your request type.

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

No branches or pull requests

3 participants