Considerations for using static methods in PHP _ PHP Tutorial

Source: Internet
Author: User
Several considerations for using static methods in PHP. Several considerations for using static methods in PHP This article introduces several common considerations for using static methods in PHP. Share it with you for your reference. The specific method is as follows: 1. Several precautions for using static methods in PHP

This article introduces several common precautions for using static methods in PHP. Share it with you for your reference. The specific method is as follows:

1. even if the methods in the class are not declared as static, but they do not use variable members of the changeable class, they can still be called using the external operator;

2. the value of $ this in the method called in static mode (with: operator) is determined by the context during the call! Instead of defining its class !!

For example, the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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 .'
'. 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 ();

}

}

$ T2 = new TestClass2 ();

$ T2-> test_func2 ();

What is the output of this code? I thought it would be normal_v from TestClass1.
STATIC_V from TestClass1. the test shows that I am 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. the internal $ this variable is determined by TestClass2!

In fact, the relationship between these two classes should belong to "two-way Association ".

If you are interested, you can test and run this article. I believe there will be new gains!

Examples in this article describes several common precautions for using static methods in PHP. Share it with you for your reference. The specific method is as follows: 1. even if the class...

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.