I am refreshing myself on OOP with PHP and I saw an example of setting functions and/or variables as static. When and why would I set a variable/function to static? I've done other languages and don't really recall ever using static, I never found a real purpose to it. I know what it does but why not just use a variable instead?
相关问题
- Views base64 encoded blob in HTML with PHP
- how to define constructor for Python's new Nam
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Keeping track of variable instances
Static functions and variable are used to access Variables/functions in a global scope, like this:
When something is static, it can be accessed anywhere, and is very similar to a procedural type function, except it can use self and is contained in the classes scope.
One of the ways to use this is to keep only one instance of a class, or a Singleton Method.
Because it has a private constructor, it cannot be instantiated with the
new
operator, so you are forced to callmyClass::get_connection()
to get a class. That function can make the new class (because it is a member of the class). Then it stores the class in a static variable, and if you call the function again, it will simply returned the created class.In the end, the static keyword is used to keep things, well, static, in reference to scope. It means you don't want anything 'changing' because of the current scope. While the Singleton method stretches this a little, it keeps with the same idea that you always have the 'same' class, not matter what scope you are in.
PHP Documentation
Static Keyword
Scope Resolution Operator
StackOverflow Knowledge
How to Avoid Using PHP Global Objects
Share Variables Between Functions in PHP Without Using Globals
Making a Global Variable Accessible For Every Function inside a Class
Global or Singleton for Database Connection
PHP Classes: when to use :: vs. -> ?
Visit: http://verraes.net/2014/06/when-to-use-static-methods-in-php/
Static elements have a number of characteristics that can be useful.
First, they are available from anywhere in your script (assuming that you have access to the class). This means you can access functionality without needing to pass an instance of the class from object to object or, worse, storing an instance in a global variable.
Second, a static property is available to every instance of a class, so you can set values that you want to be available to all members of a type.
Finally, the fact that you don’t need an instance to access a static property or method can save you from instantiating an object purely to get at a simple function.
Generally using static function you can optimize the speed as well as memory and the scope of method should not be changed its should be static in nature and you can access the objects static properties ,methods without initiating them so saves the memory in mean time.
You use static when you want to use a method / variable that is not tied to an instance. That can happen when :
There is no relation with your purpose and an instance (useful for toolboxes in languages that doesn't allow anything else that OOP like Java, but not useful in PHP).
You want to control the access to the instance. Most often, the instance you want to deal with is not defined when you write the code, but will be at execution. The Singleton pattern is the best example. You can use static methods as factories to create an object according to the context or sharing resources with other instances. E.G : a static member can give access to a data base layer so part of the app accesses the same one from anywhere and it's opened/closed without conflicts.
Performances matter and the method will be executed a lot of times. In that case, you will save some CPU time preventing the interpreter from looking up the member to an instance at each call. But still, if perfs becomes such an issues that you come to this solution, it might time to reconsider your architecture, or the use of a binding to a faster language for the critical parts of the code.
You have a method that is related to a type but will be applied to another. It can make sense to write the method into the declaration of the first type, but set it static since it expects an instance of the another one.
The perfect example is a String parser :
If you want to share data with all instances, like counter for number of instances created on current execution .