Calling multiple methods in the same instance of a PHP class

Asked

Viewed 128 times

1

I wonder how I can do that:

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

I have a View class (MVC) in which I step 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’d like to keep it that way:

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

1 answer

2


The assign method has to 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; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.