When should I use static methods in a class and what are the benefits?

前端 未结 10 1439
孤独总比滥情好
孤独总比滥情好 2020-11-27 12:34

I have concept of static variables but what are the benefits of static methods in a class. I have worked on some projects but I did not make a method static. Whenever I need

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 12:59

    Syntax (php) for static methods:

    
    

    Client code:

    echo Number::multiply(1, 2);
    

    Which makes more sense than:

    $number = new Number();
    echo $number->multiply(1, 2);
    

    As the multiply() method does not use any class variables and as such does not require an instance of Number.

提交回复
热议问题