NotionCommotion Posted December 26, 2018 Share Posted December 26, 2018 Often, I see "get" and "set" used in a function name, and the intent is obvious. Other times I see "with" (i.e. PSR-7's withScheme, withUserInfo, withHost, withPort, withPath, withQuery, withFragment). It appears to indicate just to clone itself ($this), apply the argument, and return the new object. Am I correct? Thanks 1 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 26, 2018 Share Posted December 26, 2018 What does the (appropriate) manual say it means? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 27, 2018 Author Share Posted December 27, 2018 As far as I can tell, it doesn't. https://www.php-fig.org/psr/psr-7/ Quote Link to comment Share on other sites More sharing options...
requinix Posted December 27, 2018 Share Posted December 27, 2018 "with" isn't some special programming term in PHP. Apply your understanding of the English language to the PHP code and see if you can understand why they decided to name their methods that way. 1 Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 27, 2018 Author Share Posted December 27, 2018 Yes, I recognize that "with" isn't a programming term. I've seen it used elsewhere and not just by psr7, and expect that other specifications and frameworks/libraries/etc will use the English definition of the word the same way. In my original post, I stated that I believe it to indicate that a cloned copy of an object will be returned but with a single changed property value, and I am now almost certain this is correct. Is it common for other frameworks to use this same definition? Quote Definition of with 1a: in opposition to : AGAINSThad a fight with his brother b: so as to be separated or detached frombroke with her family 2a—used as a function word to indicate a participant in an action, transaction, or arrangementworks with his fathera talk with a friendgot into an accident with the car b—used as a function word to indicate the object of attention, behavior, or feelingget tough with himangry with her c: in respect to : so far as concernson friendly terms with all nations d—used to indicate the object of an adverbial expression of imperative forceoff with his head e: OVER, ONno longer has any influence with them f: in the performance, operation, or use ofthe trouble with this machine 3a—used as a function word to indicate the object of a statement of comparison or equalitya dress identical with her hostess's b—used as a function word to express agreement or sympathymust conclude, with you, that the painting is a forgery c: on the side of : FORif he's for lower taxes, I'm with him d: as well ascan pitch with the best of them 4a—used as a function word to indicate combination, accompaniment, presence, or additionheat milk with honeywent there with herhis money, with his wife's, comes to a million b: inclusive ofcosts $5 with the tax 5a: in the judgment or estimation ofstood well with her classmates b: in or according to the experience or practice ofwith many of us, our ideas seem to fall by the wayside— W. J. Reilly 6a—used as a function word to indicate the means, cause, agent, or instrumentalityhit him with a rockpale with angerthreatened with tuberculosishe amused the crowd with his antics barchaic : by the direct act of 7a—used as a function word to indicate manner of actionran with effortacknowledge your contribution with thanks b—used as a function word to indicate an attendant fact or circumstancestood there with his hat on c—used as a function word to indicate a result attendant on a specified actiongot off with a light sentence 8a(1): in possession of : HAVINGcame with good news (2): in the possession or care ofleft the money with her mother b: characterized or distinguished bya person with a sharp nose 9a—used as a function word to indicate a close association in timewith the outbreak of war they went homemellows with time b: in proportion tothe pressure varies with the depth 10a: in spite of : NOTWITHSTANDINGa really tip-top man, with all his wrongheadedness— H. J. Laski b: except forfinds that, with one group of omissions and one important addition, they reflect that curriculum— Gilbert Highet 11: in the direction ofwith the windwith the grain Quote Link to comment Share on other sites More sharing options...
ginerjm Posted December 27, 2018 Share Posted December 27, 2018 And once again you are into something that most of us have never dealt with! So what is it that a person like you is doing with this new-found mystery? Or are you in fact simply a hunter of the off-beat and curious ephemera, set on sharing your discoveries with this PHP-related community? Do you really have a need to use all these bits and pieces in your programming efforts? 1 Quote Link to comment Share on other sites More sharing options...
requinix Posted December 28, 2018 Share Posted December 28, 2018 13 hours ago, NotionCommotion said: In my original post, I stated that I believe it to indicate that a cloned copy of an object will be returned but with a single changed property value, and I am now almost certain this is correct. Yes. 13 hours ago, NotionCommotion said: Is it common for other frameworks to use this same definition? If they use the word "with" then the code tends to have a similar meaning. Or they may do similar code but not name it "with". In fact I'd say it's more common not to use "with" and instead to name the methods normally and then support method chaining. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 28, 2018 Author Share Posted December 28, 2018 43 minutes ago, requinix said: In fact I'd say it's more common not to use "with" and instead to name the methods normally and then support method chaining. I tend to agree, but didn't want to oppose a better standard. I think the reason they do so is they wish to enforce immutability (which I see value but am still undecided). Quote Link to comment Share on other sites More sharing options...
requinix Posted December 28, 2018 Share Posted December 28, 2018 The PSRs have to work in terms of interfaces which means all operations must be methods, so setting a URL scheme would need a method. To support chaining the method can return an instance of the class, but that doesn't imply it's immutable: there could very well be something like a Url class and an ImmutableUrl class, and they would both behave the same way. Immutability would be primarily stated through documentation - or a class name. It's also implied to a lesser extent through coding: if a Url::foo() method returns an instance of Url, calling code should assume that the return value could be a new instance. Immutability is mostly a form of defensive programming, guaranteeing that an instance of an object in one place cannot be modified in another place. There are times when it is a technical requirement, mostly because underlying data can/should not be modified in place, but with high-level languages like PHP they're fairly rare. Remember the PSRs are primarily for framework interoperability - so that they all use a similar (if not identical) implementation of a common concept, so they can mesh a little better with each other. They are not a PHP community standard. There's nothing saying you need to, or even should, abide by their decisions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.