1

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

Jase Leow
  • 91
  • 1
  • 9
  • 1
    *"I'm having trouble finding documentation for `with`."* -- take it as a hint and try to guess. – axiac Jun 19 '17 at 17:05
  • @axiac Logically it would either mean I have entered in insufficient search phrase, or no documentation exists. It also means I want to throw my computer out the window, but that's rather less logical. – Jase Leow Jun 19 '17 at 17:09
  • @JaseLeow Indeed, `with` is a common English word and the search engines usually ignore it or assign it a low value. However, searching on http://php.net should bring you at some point to the language definition page where all the keywords are listed. – axiac Jun 19 '17 at 17:23

5 Answers5

4

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

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
2

https://laravel.com/docs/5.2/helpers#miscellaneous

with() is a helper function that just returns the object.

A normal use case I've seen is when you're cloning an object, it allows you to chain onto that clone:

$object = new Object();
with(clone $object)->doSomethingWithoutAffectingTheOriginal();

In the use case you've provided, there is no difference. with() is completely redundant if you've wrapped the creation of the instance in parentheses.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
2

Thanks to the answers by various posters, I've realised it is a function in Laravel. Here is the Laravel source: Taken from vendor/laravel/framework/src/Illuminate/Support/helpers.php

if (! function_exists('with')) {
    /**
     * Return the given object. Useful for chaining.
     *
     * @param  mixed  $object
     * @return mixed
     */
    function with($object)
    {
        return $object;
    }
}

From that terse comment there, I understand it is used for chaining, e.g. queries together. (I use the term 'understand' loosely.)

Jase Leow
  • 91
  • 1
  • 9
1

tldr

The with function has been used to chain methods, but is no longer required. Since php 5.4 you can simply write (new MyObject)->myMethod() - this was not possible prior to v5.4:

Example: https://3v4l.org/57dc0#v5.3.29

More detailed answer

The with was a workaround to use the instance without assigning it to a variable:

$instance = new A;
$instance->doSomething();

Using the with function (works prior to v5.4 - example):

with(new A)->doSomething();

Since v5.4 you can simply write:

(new A)->doSomething();
SirPilan
  • 4,649
  • 2
  • 13
  • 26
0

with is not a PHP keyword.

Also take a look at the language reference page.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • @DonkeyKingandDonkeyKing really? What about the accepted answer? Should it be a comment too? – axiac Aug 23 '17 at 06:08