search

Home  >  Q&A  >  body text

laravel - pros and cons of php static functions and public methods

Benefits of static functions: 1. Object-oriented 2. Resident in memory, faster 3. Easy to manage
Such as Yii::app()->params[$name];
Benefits of public methods: 1. Easy to call
such as app_param($name);
When setting up a function, how to choose to create it as a static function or a public method? Or is object-oriented development now popular?

typechotypecho2709 days ago852

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-06-26 10:51:16

    The two have different scopes and different uses. First let’s look at the syntax for calling static functions,
    Static functions:

    类名:方法

    Public functions:

    方法
    

    Static functions rely on classes, while methods are used globally. For example, if you need a method to convert Chinese to English, in this case you must put it in a public method, because it is very versatile and has very few dependencies. To give another example, I want to get the number of blog followers anywhere, Blog:subscribe(). At this time, you can use new (not to mention the trouble, the execution efficiency is not as good as the static class) or put it in a public function. Not suitable, no doubt static class is the best choice.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-26 10:51:16

    There is not much difference in performance consumption between the two. Generally speaking, if it is a method that needs to be used globally but also requires class attributes, it will be used as a static method. If it is used globally and does not require class attributes, it can be used directly as A function is OK. This is an important consideration in code planning. For example, if you are more inclined to be more reasonable and standardized in object-oriented, then try to reduce static methods as much as possible and make the code specifications more unified. Nowadays, business development is more object-oriented, which is faster and more maintainable.

    reply
    0
  • Cancelreply