Jump to content
NotionCommotion

What does "with" imply

Recommended Posts

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

  • Like 1

Share this post


Link to post
Share on other sites

What does the (appropriate) manual say it means?

Share this post


Link to post
Share on other sites

"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.

  • Like 1

Share this post


Link to post
Share on other sites

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

2aused 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

bused 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

dused 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

3aused as a function word to indicate the object of a statement of comparison or equalitya dress identical with her hostess's

bused 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

4aused 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

6aused 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

7aused as a function word to indicate manner of actionran with effortacknowledge your contribution with thanks

bused as a function word to indicate an attendant fact or circumstancestood there with his hat on

cused 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

9aused 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

 

 

Share this post


Link to post
Share on other sites

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?

  • Like 1

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.