What is the difference between a standalone custom function outside a class and a static method in a class?

Source: Internet
Author: User
function Test () {... return $result;} function test1 () {... return $result;} .... function testn () {.... return $result;}. Class Funs{static function Test () {... return $result;} static function Test1 () {.... return $result;} .... static function Testn () {.... return $result;}}. $a = Test (); $b = Funs::test () ...




What is the difference between a separate function written outside a class and a static function written inside a class? Is the function outside the class static? Also like in the tutorial, the static method of saving memory? If they want to compare efficiency, which is high?

Is it good to write a commonly used function in a class as a static method?

I am a novice, please hand to give the correct advice!


Reply to discussion (solution)

Static methods can only access static properties, as constrained by php5.3
Well, there's no big difference between a static method and a normal function.
In fact, this singular writing will gradually be replaced by the namespace
Of course, some people would say that different classes can define static methods with the same name to achieve differentiation
But namespaces are used for this purpose.

In accordance with the principles of the PHP line? Understanding:
PHP every time you write a function outside of a class he will go through it, and the one written in class does not traverse, but from then on I feel that the static package speed will be faster than the external function.
The OOP approach is currently respected, so it is recommended to encapsulate function into class as a static method. It's not too tedious to use.

At least mine. The method is all encapsulated in class as a static method, such as:
Class abc{
public static function ff ($a) {
echo $a;
}
}
ABC::FF (' 23 ');

Well, the feeling of putting functions in a class is a static implementation of the collation, there is a sense of the rules, such as the function of the file is placed in the files class and so on, and then used when the system __autoload automatic calls do not have to manually include

But see a lot of more well-known procedures, not to do so, novice very confused hehe

The difference between the two can be called in the class and the class outside the call to try to understand. Some questions to discover and test yourself far more impressive than to ask!

function Test () {... return $result;} function test1 () {... return $result;} .... function testn () {.... return $result;}. Class Funs{static function Test () {... return $result;} static function Test1 () {.... return $result;} .... static function Testn () {.... return $result;}}. $a = Test (); $b = Funs::test () ...




What is the difference between a separate function written outside a class and a static function written inside a class? Is the function outside the class static? Also like in the tutorial, the static method of saving memory? If they want to compare efficiency, which is high?

Is it good to write a commonly used function in a class as a static method?

I am a novice, please hand to give the correct advice!

A change in the idea of programming, without any distinction

My opinion and the old Xu approach, the namespace is trending
It's just that some script parsing languages are slow to handle namespaces (just relative), but I think it will improve later.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.