This article mainly introduces the use of static methods of PHP several considerations, in the form of an example of Php static method call tips and error-prone analysis, the need for friends can refer to the next
The examples in this article describe several common considerations for using static methods in PHP. Share to everyone for your reference. Here's how:
1. even if the method in the class is not declared with static, but it is not used to change the class member variable, the operator is still available externally:: To invoke ;
2. The $this value in a method called statically (by: operator) is determined by the context of the call! Instead of defining his class !!
For example, the following code:
<?php class TestClass1 {public $normal _v = ' normal_v from TestClass1 '; public static $STATIC _v = ' static_v from TestClass1 '; Public Function test_func1 () { echo $this->normal_v. ' <br/> '. Self:: $STATIC _v; }} class TestClass2 {public $normal _v = ' normal_v from TestClass2 '; public static $STATIC _v = ' static_v from TestClass2 '; Public Function Test_func2 () { testclass1::test_func1 ()} } $t 2 = new TestClass2 (); $t 2->test_func2 ();
This code is what the output will be, I thought it would be normal_v from TestClass1 <br/> Static_v from TestClass1, the test found that I was wrong, the correct output is:
Normal_v from TestClass2
Static_v from TestClass1
Note: Although TEST_FUNC1 () is defined in TestClass1, it is called in TestClass2, and its internal $this variable is determined by TestClass2!
In fact, these two-class relationships should belong to the "two-way association."
Interested friends can test run the example of this article, I believe there will be a new harvest!