Calling multiple methods on the same instance of a PHP class

1

I would like to know how I can do this:

Class::methodA()->methodB()->methodC()...;

I have a View class (MVC) in which I pass parameters to create variables (assign). Currently it looks like this:

View::assign('var1', 'Variável 1');
View::assign('var2', 'Variável 2');
View::render('layout');

I would like it to look like this:

View::render('layout')->assign('var1', 'Variável 1')->assign('var2', 'Variável 2')
    
asked by anonymous 02.03.2017 / 15:26

1 answer

2

The assign method must return the class itself:

if the method is public:

public static function assign () {   return $ this; }

If the method is public static:

public static function assign () {   $ class = new Class ();   return $ class; }

    
02.03.2017 / 17:23