. Net programmers learn PHP: static keywords

Source: Internet
Author: User
Tags learn php
ArticleDirectory
    • Declaration scope.
    • Call Method
    • Lifecycle
    • Summary

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

StaticKeywords are very common in C # programming. They are used to modifier declarations that belong to types rather than static members of specific objects.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.

SoStaticWhat are the differences between keywords in 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.StaticKeyword. 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:

 <?  PHP
Function Test (){
Static $ Var1 = 1 ;
$ Var1 + = 2 ;
Echo $ Var1 . ' & Nbsp ' ;
}
Test ();
Test ();
Test ();
?>

CodeOutput: 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

  <?  PHP
Class Testc {
Public Static $ Var1 = 1 ;
Public $ Var2 = 1 ;
Function T1 (){
Self :: $ Var1 + = 2 ;
Echo Self :: $ Var1 . ' & Nbsp ' ;
Echo $ This -> Var2 . ' & Nbsp ' ;
}
Public Static Function T2 (){
Self :: $ Var1 + = 2 ;
Echo Self :: $ Var1 . ' & Nbsp ' ;
}
}
$ 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 cover static functions or variables of the parent class. (from the perspective of C # programmers, I think PHP makes things complicated.) 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

  <?  PHP
Class Test1 {
Public Static $ Var1 = 1 ;
Public Static Function T2 (){
Self :: $ Var1 + = 2 ;
Echo Self :: $ Var1 . ' & Nbsp ' ;
}
}
Class Test2 Extends Test1 {
Static $ Var1 = 'Hello ';
Static Function T2 (){
Parent :: T2 ();
Echo Self :: $ Var1 . ' & Nbsp ' ;
}
}
Test1 :: T2 ();
Test2 :: T2 ();
?>

Output result: 3 5 hello

It is best that, based on the above example, we can easily think that the Child class can use the parent keyword to access the parent class. How can the parent class access the static method of the Child class? 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

  <?  PHP
Class Test1 {
Function T1 (){
Static :: T2 ();
}
Public Static Function T2 (){
Echo Self :: ' Test1 & nbsp ' ;
}
}
Class Test2 Extends Test1 {
Static Function T2 (){
Echo Self :: ' Test2 & nbsp ' ;
}
}
$ 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.