[Go] singleton mode with static variable in PHP

Source: Internet
Author: User

In PHP, there is no static variable in the general sense. Unlike Java, C + +, the survival period of static variables in PHP is only one session per PHP, so there is no such thing as a static variable of Java or C + +.

So, in PHP, the existence of a static variable is simply passing a variable in a struct (a method or class) whose scope is within this file.

Well, look at an example.

1

2

3

4

5

6

7

8

9

10

11

function Test () {

static $var = 1;

echo $var + +. ' <br/> ';

}

Test ();

Test ();

Test ();

OutPut

1

2

3

In the three invocations of the function test, the variable $var is present in three calls and increments by 1 each time without emptying or resetting

Therefore, it can be concluded that static variables exist in the life cycle of the current structure. In the current example, the life cycle of the test function is the current PHP script, as long as the program is not released is valid.

And in the class, the code is probably like this.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

Class A

{

private static $a = 1;

Private $b = 2;

Public function Add ()

{

echo Self:: $a + +. ' <br/> ';

echo $this->b++. ' <br/> ';

}

}

$class 1 = new A ();

$class 1->add ();

$class 1->add ();

$class 2 = new A ();

$class 2->add ();

$class 2->add ();

Output

1

2

2

3

3

2

4

3

From the running result of the class above, we get the same result in the function.

Well, perhaps a summary is

The static variables of PHP exist permanently in the life cycle of the corresponding structure, and the values remain the same regardless of how many times the struct has been invoked or instantiated.

In fact, this is the difference between dynamic variables and static variables, see this article specifically. Dynamic variables are only valid in the class, while static variables are in the current PHP script.

And look at the singleton pattern.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

Class A

{

private static $instance = null;

Private $b = 1;

public static function Get_instance ()

{

if (self:: $instance = = null) {

$classname = __class__;

Self:: $instance = new $classname ();

}

Return self:: $instance;

}

Public function Add ()

{

$this->b++;

}

Public Function Show ()

{

Echo $this->b;

}

}

$a = a::get_instance ();

$b = A::get_instance ();

Here a $ A and $b variables are exactly the same!

$a->add ();

$a->show ();

echo ' <br/> ';

$b->show ();

Output

2

2

At this point, because singleton mode exists, so that $ A and $b are exactly the same object, so if you need to share the data, there is no need for static variables (nonsense, it is their own.) Because at any time, only one instance of this class exists in the application! No matter how many times you invoke the singleton, the data inside will not be re-instantiated. )

Therefore, in the singleton mode, static variables simply do not have the meaning of existence. Of course, if you have nothing to do, it is OK to use the new method to initialize the object, at which point the singleton mode is broken and returned to a state without singleton mode.

If you want to prevent the object from being instantiated with new, consider setting the __construct function of the class to the private property

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

Class A

{

private static $instance = null;

Private $b = 1;

Private Function __construct ()

{

Code in this function

Could not being get out of the class

}

public static function Get_instance ()

{

if (self:: $instance = = null) {

$classname = __class__;

Self:: $instance = new $classname ();

}

Return self:: $instance;

}

Public function Add ()

{

$this->b++;

}

Public Function Show ()

{

Echo $this->b;

}

}

$a = a::get_instance ();

$b = A::get_instance ();

Here a $ A and $b variables are exactly the same!

$a->add ();

$a->show ();

echo ' <br/> ';

$b->show ();

Output

2

2

If you try to instantiate with new,

$c = new A ();

Output

Fatal Error:call to Private a::__construct () from invalid context in

If you need to instantiate an object of Class A, you can only initialize it through an open get_instance static method.

Pros: Singleton mode avoids a large number of new operations, because each new operation consumes memory resources and system resources

Disadvantage: In PHP, all variables, whether global variables or static members of the class, are page-level, each time the page is executed, will re-establish a new object, will be emptied after the page executes, so it seems that PHP singleton mode is meaningless, So PHP singleton mode I think it makes sense to just have multiple scenarios for a single page-level request and need to share the same object resource.

[Go] singleton mode with static variable in PHP

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.