Skip to content
Advertisement

PHP “with” keyword – what does “with” do?

Can someone please explain what PHP “with” does?

Example begins:

Say I have a class:

Appfa_batch

What is the difference between this statement:

$w = (with (new Appfa_batch))
       // uses 'with'
       // returns Appfa_batch 

and this statement?

$n = (new Appfa_batch)
       // does not use 'with'
       // also returns Appfa_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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement