Chaining Static Methods in PHP?

前端 未结 16 2126
情歌与酒
情歌与酒 2020-11-27 14:25

Is it possible to chain static methods together using a static class? Say I wanted to do something like this:

$value = TestClass::toValue(5)::add(3)::subtrac         


        
16条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 14:52

    No, this won't work. The :: operator needs to evaluate back to a class, so after the TestClass::toValue(5) evaluates, the ::add(3) method would only be able to evaluate on the answer of the last one.

    So if toValue(5) returned the integer 5, you would basically be calling int(5)::add(3) which obviously is an error.

提交回复
热议问题