Home >Backend Development >PHP Tutorial >Can You Instantiate an Object and Call a Method on the Same Line in PHP?

Can You Instantiate an Object and Call a Method on the Same Line in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-10 04:33:02980browse

Can You Instantiate an Object and Call a Method on the Same Line in PHP?

Instantiating an Object and Calling a Method on the Same Line in PHP

In PHP, there are two ways to instantiate an object and call a method:

The second method is only available in PHP 5.4 and later. It allows you to instantiate an object and call a method on the same line, which can be more concise and readable in some cases.

This feature was added to PHP in response to user requests for a more concise way to instantiate an object and call a method. It is particularly useful for chaining method calls, as it allows you to avoid having to store the object in a variable.

The above is the detailed content of Can You Instantiate an Object and Call a Method on the Same Line in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn