In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge about "what are the static variables in php method". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Static variables in PHP only exist in function scope, that is, static variables only survive in the stack; in PHP, as long as the keyword static is added before the variable, the variable becomes static.
Operating environment: Windows 7 system, PHP 7.1 version, DELL G3 computer
What are static variables in php methods?
Static variables and static variables in PHP
Static variables only exist in function scope, that is, static variables only exist on the stack. Common variables within a function are released at the end of the function, such as local variables, but static variables are not. That is, the next time the function is called, the value of the variable is preserved.
If you add the keyword static to a variable, it becomes static.
Program Run Results:
1
2
2
4
3
8
After the function test() is executed, the value of the variable $nm is saved.
Static attributes are often used in classes, such as static members and static methods.
Program List: Static members of a class
The static variable $nm belongs to the class nowamagic, not to an instance of the class. This variable is valid for all instances.
:: is a scope-qualified operator, using the self scope instead of the $this scope, where the $this scope represents only the current instance of the class, and self:: represents the class itself.
Program Run Results:
1
3
2
5
Program List: Static Properties
Program Run Results:
www.nowamagic.net
www.nowamagic.net
www.nowamagic.net
www.nowamagic.net
Program List: Simple static constructor
PHP has no static constructor, you may need to initialize static classes, there is a very simple way to call the Demonstration() method of the class directly after the class definition.
Program Run Results:
This is the result of demonstration()
The following is an introduction to the use of PHP static variables.
The static keyword is common in C#programming and is used as a modifier to declare static members that belong to the type itself rather than to a particular object. The static modifier can be used with classes, fields, methods, attributes, operators, events, and constructors, but not with indexers, destructors, or types other than classes. Classes, functions, and variables declared static will not be able to reference instance methods or variables, and once the static modifier is added to a class in C#, all internal variables and methods must be static. Static variables and methods must be referenced by class names and not by instance objects.
What is the difference between static in PHP and C#?
claimed range
In PHP static variables are used more broadly than in C#. We can not only add static modifiers to classes, methods, or variables, but we can even add static keywords to internal variables of functions. A variable added with the static modifier does not lose its value even after the function is executed, that is, the variable remembers its original value the next time the function is called. For example:
The results are as follows:
3 5 7
One thing to note here is that assignment to a variable is called only when the variable is initialized for the first time, and not later in the execution of the function.
Since PHP functions are also first-class citizens, unlike C#, we can define functions directly and call them directly anywhere in the code, which is somewhat similar to javascript. Therefore, static variables of functions are more useful than global variables at this time, which can avoid conflicts caused by repeated definitions of variables. Since a function cannot be defined and called directly in C#, it must be hosted in a class, so if the function requires static variables, we only need to define them in the class to achieve the same effect.
calling method
In C#, the way we call 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#accesses its static members through class names. Methods (variables) are performed. And in C#, static functions are functions that cannot be set to virtual methods or overridden. PHP provides more flexible support for this.
First of all, we know that instance methods are called in PHP via someobj->someFun(), so can we call static functions via SomeClass->someFun() like C#? In PHP, static members can only be called via:: SomeClass::someFun().
The results are as follows:
3 1 5
Another difference from C#is that in a method in a class, if we need to call a static variable, we have to call the static variable self::$somVar (note the $sign in front of the variable, instance variables are not needed), while calling the static method is self::someFun()(the $sign is not needed here). The example above.
In addition, the biggest difference with C#is that in PHP, subclasses can override static functions or variables of the parent class, not only that,(from the C#programmer's point of view, I think PHP will make things more complicated), because the default self::staticFun() calls static functions of subclasses, what if we want to call static variables of the parent class at this time? Here PHP provides an extra parent to invoke static members of the base class. For example:
The results are as follows:
3 5 'Hello'
Best of all, it's easy to imagine from the example above that a subclass can access its parent class using the parent keyword, so how does the parent class access its static methods? Another use of static is given here, where PHP calculates the final static method based on the inheritance hierarchy of the class if the scope before the static method being invoked is changed to static. For example:
The results are as follows:
Test2 Test2
Here, when the t1 method calls the t2 static method, the t instance will find the final static method according to its instance and output Test2.
summary
From the above analysis, it is not difficult to see that PHP provides more power or flexibility than C#for the use of static members, but from my point of view, this flexibility is not necessarily better, from a certain point of view, if the inheritance hierarchy of classes is too complex, it may confuse me. Of course, the same tools can be used differently by different people, and since PHP offers more options, I believe that static in PHP may provide a more powerful and easy way to use them than in C#if used properly.
"What are static variables in php methods" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.