Skip to content
Advertisement

How to call functions within a class like Symfony QueryBuilder

I’m trying to create a PHP class and when I call its functions I want them to be called this way.

JavaScript

class example:

JavaScript

Now I want to call it this way:

JavaScript

I’ve seen this in PDO and in Symfony QueryBuilder. If there’s a way to do this please tell me. Thanks in advance.

Advertisement

Answer

In order to achieve method chaining you should return $this instead of returning static::$name; as in function getName(). You should also consider making your class properties private or protected in case you use inheritance to avoid modification from outside

JavaScript

You will get the output as: SELECT name,age FROM users

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