newQuery(); I have never seen the...">

What is the "c" used for PHP?

I came across this line in the eloquent ORM library:

return with(new static)->newQuery(); 

I have never seen the "c" that was used before, and cannot find it in the PHP documentation. I assume c is the stop word in most queries, so I donโ€™t even get close.

Having never come across a โ€œforโ€ many years of PHP programming, I feel like I'm missing out. What does it do? I came across one missing comment regarding ORM, which mentioned that "c" is no longer needed in PHP-5.4, but that was as much as was said. If this is accurate, it would be nice to know what the equivalent of PHP-5.4 is.

Update: Details supporting the answer: -

I found this helper function in the Larvel Immuminate / Support / helpers.php helper script:

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

as mentioned in several answers. This global function allows you to create an object, and the methods are executed in t in one expression. It (somehow) is registered in the composer autoload_files.php script when Laravel is installed, so it loads on every page, although it does not contain classes.

Thanks to everyone. It pays not to assume that everything should be a class with names in a modern framework.

+6
source share
3 answers

This is a function that will look something like this:

 function with($obj) { return $obj; } 

This is a shorter version and a more readable version for new ExampleObj()->newQuery() . The with function is not built into PHP. This is a workaround for older versions than PHP 5.4. As @Rocket Hazmat pointed out in PHP 5.4+, you can only do new ExampleObj()->newQuery() , so the with function allows you to maintain readable backward compatibility.

+8
source

The with function is an assistant provided by Laravel, as described here . The best documentation is code and, as you can see, it simply returns an object. In 5.4 / 5.4, it is best for you to surround the expression in parens to avoid the overhead of an unnecessary function call.

+3
source

I think @Izkata hit a nail on the head in the comments.

[I assume] this is a workaround for something in the lines new Foo()->newQuery() not working in some version of PHP

In PHP 5.4, the following has been added:

Added access to a class member when creating an instance, for example. (new Foo)->bar() .

(Source: http://www.php.net/manual/en/migration54.new-features.php )

So, I would suggest that with looks something like this:

 function with($x){ return $x; } 

In PHP 5.4+ you can do (new static)->newQuery() , but with an older version you cannot. with probably exists, so you can do with(new static)->newQuery() , which will work in any version of PHP.

+3
source

Source: https://habr.com/ru/post/957840/


All Articles