. NET programmers learn PHP: static keywords

Source: Internet
Author: User
I have been learning PHP for some time. I think it is necessary to summarize some things. for NET programmers, many concepts in PHP are different from previous understandings. Here we will focus on analyzing static keywords. Static keywords are very common in C # programming. They are used as modifiers to declare static

I have been learning PHP for some time. I think it is necessary to summarize some things. for NET programmers, many concepts in PHP are different from previous understandings. Here we will focus on analyzing static keywords. Static keywords are very common in C # programming. They are used as modifiers to declare static

I have been learning PHP for a while. Some things still need to be summarized. NETProgrammerMany PHP concepts are different from previous ones. Here we will focus on the analysis.StaticKeywords.

StaticKeywordsIt is very common in C # programming. It is used to declare a type rather than a static member of a specific object.StaticModifiers can be used for classes, fields, methods, attributes, operators, events, and constructors, but not for indexers, destructor, or types other than classes. DeclaredStaticClasses, functions, and variables cannot reference instance methods or variables. In addition, once the class is added to C #,StaticModifier, all the internal variables and methods must be static. Static variables and methods must be referenced by class names instead of instance objects.

SoStaticKeywordsWhat are the differences between php and C?

Declaration scope.

For C #, PHPStaticThe use of variables is wider. We can not only add them before classes, methods, or variables.StaticModifier, which can even be added to the internal variables of the function.StaticKeywords. AddedStaticThe modifier variable will not be lost even after the function is executed. That is to say, the variable will still remember the original value when the function is called the next time. For example:

function test(){
static$var1=1;
$var1+=2;
echo$var1.' ';
}
test();
test();
test();
?>

Code output: 3 5 7

Note that the value assignment operation of a variable will only be called during the first initialization of the variable. This operation will not be called during subsequent function execution.

Since functions in PHP are also a first-class citizen, unlike C #, we can directly define functions and call them directly anywhere in the code, which is somewhat similar to javascript. Therefore, static variables of a function are more useful than defining global variables. They can avoid conflicts caused by repeated definitions of variables. Since the function in C # cannot be directly defined and called, it must be embedded in the class. Therefore, if the function requires static variables, we only need to define them in the class to achieve the same effect.

Call Method

In C #, the method for calling static members is very simple and consistent, because static members do not belong to instance objects, so whether it is a method or a variable, C # access to its static members is by class name. method (variable. In C #, static functions cannot be set as virtual methods or overwritten. PHP provides more flexible and diverse support.

First, we know that PHP calls the instance method through someobj-> someFun (), so can we call static functions through SomeClass-> someFun () like C () what about calling? The answer is no. in PHP, the call to static members can only be done through:, for example, SomeClass: someFun ().

Code

class TestC{
publicstatic$var1=1;
public$var2=1;
function t1(){
self::$var1+=2;
echo self::$var1.' ';
echo$this->var2.' ';
}
publicstaticfunction t2(){
self::$var1+=2;
echo self::$var1.' ';
}
}
$t=new TestC();
$t->t1();
TestC::t2();

Result output: 3 1 5

Another difference from C # Is that in methods in the class, if we need to call static variables, we must use the self: $ somVar static variable (note the $ symbol before the variable, instance variables are not required), while static methods are called as self: someFun () ($ is not required here ). For example.

In addition, the biggest difference with C # Is that in PHP, subclasses can overwrite static functions or variables of the parent class #ProgrammerFrom the point of view, I think PHP is actually complicated.) because the default self: staticFun () calls the static function of the subclass, what if we want to call the static variables of the parent class at this time? Here, PHP provides additional parents to call static members of the base class. For example:

Code

class Test1{
publicstatic$var1=1;
publicstaticfunction t2(){
self::$var1+=2;
echo self::$var1.' ';
}
}
class Test2 extends Test1{
static$var1=‘Hello’;
staticfunction t2(){
parent::t2();
echo self::$var1.' ';
}
}
Test1::t2();
Test2::t2();
?>

Output result: 3 5 Hello

It is best that, based on the above example, we can easily think that parent can be used to access the parent class by subclass.KeywordsSo how does the parent class access the static method of the subclass? Here we provideStaticHere, if you change the scope of the called static methodStaticPHP calculates the final static method based on the hierarchy of the class. For example:

Code

class Test1{
function t1(){
static::t2();
}
publicstaticfunction t2(){
echo self::'Test1 ';
}
}
class Test2 extends Test1{
staticfunction t2(){
echo self::'Test2 ';
}
}
$t=new Test2();
$t->t1();
Test2::t2();
?>

The output result is Test2 Test2.

Here, t instance finds the final static method based on its instance and outputs test2.

Lifecycle

Because. NET and PHP are essentially different, so static is obviously different in the two languages. in NET, static exists along with the life cycle of the program, that is, any. NET Program, whether it is ASP. NET Web programs are still common WinForm programs. As long as the program starts, the static value will be retained. For example, when we access a Web site, if there is a static variable in the int access count to indicate the number of visitors, the static variable value will not be reset as long as IIS and the process are not stopped. However, in PHP, the static value only exists in one user access process. That is to say, the static life cycle is the whole process from the user sending a request to the server to return the final view result, after that, all static variables will be released by the server until the next time the user sends a new request for reinitialization.

Summary

From the above analysis, we can easily see that for the use of static members, PHP provides more powerful functions or flexibility than C #, But from my perspective, this flexibility is not necessarily better. From a certain perspective, if the inheritance hierarchy of the class is too complex, it may confuse me. Of course, the use of the same tool varies with different people. Since PHP provides more diverse options, I believe that if appropriateStaticIt may provide more powerful and easy-to-use methods than in C.

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.