Home  >  Article  >  Backend Development  >  PHP object-oriented programming (oop) study notes (2) - Properties and methods of static variables and delayed binding_PHP tutorial

PHP object-oriented programming (oop) study notes (2) - Properties and methods of static variables and delayed binding_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:20731browse

Static (static) keyword is used to define static methods and properties. static can also be used to define static variables and late static binding.

1. static variable static variable

Static variables only exist in the local function scope, but their values ​​are not lost when the program execution leaves this scope. That is to say, the variable will still remember its original value the next time this function is executed. To define a variable as static, just add the static keyword before the variable.

Copy code The code is as follows:

function testing()
{
static $a = 1;
$a *= 2;
echo $a."n";
}
testing();
testing();
testing();
testing() ;
/**
 *    2
 *    4
 *    8
 *    16
 *    [Finished in 0.1s]
*/

Static variables also provide a way to deal with recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, as they may recurse indefinitely. You must ensure that there are adequate ways to terminate recursion.

In this example, the testing() function will internally save the value of the $a variable after each execution. The next time testing() is called, the value of $a is restored, and testing() multiplies this value by 2 and prints it. The initial default value of the variable is 1, and this assignment only occurs the first time the variable is initialized. This operation will not be called during each execution of the function.

2. Use of static elements in classes

There are two main uses of the static keyword in a class, one is to define static members, and the other is to define static methods. Declaring a class property or method static allows you to access it directly without instantiating the class. Static properties cannot be accessed through an object of a class that has been instantiated (but static methods can). Static properties are not accessible from objects via the -> operator. Inside a class we can use scope qualification operators to access variables at different levels of scope.

2.1, Static attributes

Since static methods do not require an object to be called, the pseudo variable $this is not available in static methods. Static variables can be thought of as belonging to the entire class rather than to an instance of the class. Different from general instance variables, static properties only retain one variable value, and this variable value is valid for all instances, which means that all instances share this property.

Copy code The code is as follows:

class MyObject
{
public static $a = 0;
function MyMethod()
{
self::$a += 2;
echo self::$a . "n";
}
}
$instance1 = new MyObject();
$instance1 -> MyMethod();
$instance2 = new MyObject();
$instance2 -> MyMethod();
/**
 *
 * 2
 * 4
 * [Finished in 0.1s]
 *
 */

The $this indicator is the current instance of the class and is a reference to the calling object.

self:: represents the class itself. When using the self:: scope qualifier, you must add the $ symbol after the operator. This operator cannot be used in code outside the class, and it cannot identify itself. Inherit the position in the tree hierarchy. When using self:: scope in an extended class, self can call methods declared in the base class, but it will always call methods that have been overridden in the extended class.

parent:: In the extended class, when the base class method is overridden, if you want to access the base class method, you can use parent::

static:: eliminates the need for us to use self:: and parent:: . You can use static when you want to point to the final class that implements the functionality. This qualifier will calculate the members of the last class in the inheritance hierarchy immediately before the code is executed.

2.3, Static method

The rules for static methods are the same as static variables. Methods can be marked as static using the static keyword, and static methods can be accessed through the name of the class and the scoping operator (::).

There is an important difference between static methods and non-static methods: when calling a static method, we no longer need to own an instance of the class.

Copy code The code is as follows:

class MyObjectBase
{
static function MyMethod()
{
static::MyOtherMethod();
}
static function MyOtherMethod()
{
echo 'called from MyObject.';
}
}
class MyExtendObject extends MyObjectBase
{
static function MyOtherMethod()
{
echo 'called from MyExtendObject .';
}
}
MyExtendObject::MyMethod();

The above example code will correctly call the MyOtherMethod method in MyExtendObject and output called from MyExtendObject. [Finished in 0.1s].

If a method does not contain the $this variable, then this method should be a static method. If you don't need an instance of a class, you should also use a static class, which eliminates the need for instantiation. In addition, the $this variable cannot be used in static methods because static methods do not belong to a specific instance.

2.4, Lazy binding

static:: eliminates the need for us to use self:: and parent:: . You can use static when you want to point to the final class that implements the functionality. This qualifier will calculate the members of the last class in the inheritance hierarchy immediately before the code is executed. This process is called delayed binding.

3. Summary

Use the static keyword to create static variables and provide a default initialization value. Static variables are modified function variables whose value remains unchanged after a function is executed.

The static keyword can also be used in classes to modify properties and methods. When used on a property, it causes the property to no longer hold a value for a certain instance, but instead holds a value for the entire class itself. Static properties can be shared among members.

To access static methods, you can use (::), which is called the scope qualifier. The left side of this operator can be a class name or a predefined scope. The predefined scope includes self parent static. The right side of the operator is a static method and property.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/788636.htmlTechArticleStatic (static) keyword is used to define static methods and properties. static can also be used to define static variables and late static Binding. 1. Static variable static variable Static variables are only in the local...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn