Saturday, January 8, 2022

[FIXED] PHP "with" keyword - what does "with" do?

Issue

Can someone please explain what PHP "with" does?

Example begins:

Say I have a class:

\App\fa_batch

What is the difference between this statement:

$w = (with (new \App\fa_batch))
       // uses 'with'
       // returns App\fa_batch 

and this statement?

$n = (new \App\fa_batch)
       // does not use 'with'
       // also returns App\fa_batch 

Context / Background Info:

I'm having trouble finding documentation for with, maybe because the PHP.net, stack overflow and google search engines considers php "with" keyword such a common search phrase.

If context helps, I came across this usage of the word with from this answer: https://stackoverflow.com/a/33222754/5722034


Solution

with is not a keyword, it's a function. The extra space between with and ( is a red herring.

The 5.2 docs include it in miscellaneous helpers. The source is available on github as well



Answered By - zzzzBov

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.