Questions about the singleton class involved in PHPUNIT testing

Source: Internet
Author: User
The singleton class is singleton in PHPUNIT testing. classA has a staticpublic $ CI attribute and a staticpublicfunction & amp; get_ci () {if (! Self: $ CI) {self: $ CI & amp; get_instance ();} r PHPUNIT test involves singleton class issues
Class A is singleton, has A static public $ CI; attribute, and has
Static public function & get_ci ()
{
If (! Self: $ CI)
{
Self: $ CI = & get_instance ();
}

Return self: $ CI;
}
Method.

Class B calls class
Class A $ CI to class B

Then, because phpunit executes multiple test cases consecutively.
Therefore, $ CI of classA remains unchanged.

Class C calls class
The returned $ CI is also an instance of class B.

Because class B has a $ abc attribute, it is 123 when class B is called.
Class C also has A $ abc attribute, which is already 456, but when class C calls class A, $ CI-> abc or 123 in class
But I expect him to be 456.

This problem occurs only when phpunit is used, because all variable objects are not cleared after a test case is completed. this problem will not occur if the test process is normal, because class C won't call class B before.

------ Solution --------------------
Discussion

In fact, there are several class D, E, F are all singleton and $ CI in it is protected or private, so it cannot be manually cleared.

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.