What are the differences between independent UDFs and static methods in classes?

Source: Internet
Author: User
What are the differences between independent UDFs outside the class and static methods in the class? Function & nbsp; test (){......... Return & nbsp; $ result;} function & nbsp; test1 (){......... Return & nbsp; $ result ;}...... What are the differences between independent UDFs outside the functio class and static methods in the class?


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 the class and a static function written in the class? Is the function outside the class static? Is it also similar to the static method mentioned in the tutorial to save memory? Which of the following is more efficient?

Is it good to write common functions in a class and define them as static methods?

I am a newbie. please give me the correct advice! Share:
------ Solution --------------------
According to php5.3 constraints, static methods can only access static attributes.
There is no big difference between static methods and common functions.
In fact, this strange writing method will gradually be replaced by namespaces.
Of course, some people may say that different classes can define static methods with the same name to achieve differentiation.
But the namespace is used for this purpose.


------ Solution --------------------
According to the principle of PHP commit:
Php will traverse the function written outside the class every time it completes the row, but the function written in the class will not be traversed, however, I feel that static encapsulation is faster than external functions.
The OOP method is currently highly respected. Therefore, it is recommended to encapsulate the function into the class to become a static method. it will not be too cumbersome to use it.

------ Solution --------------------
At least all of my methods are encapsulated into the class and become static methods, such:
Class abc {
Public static function ff ($ ){
Echo $;
}
}
Abc: ff ('23 ');
------ Solution --------------------
You can try the differences between calling in the class and calling outside the class. I was far more impressed when I found and tested some questions by myself!
------ Solution --------------------
Reference:


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(){

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.