Notes for using static methods in PHP: php static

Source: Internet
Author: User

Notes for using static methods in PHP: php static

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:

<?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();   } } $t2 = new TestClass2(); $t2->test_func2(); 

What is the output of this Code? I thought it would be normal_v from TestClass1 <br/> STATIC_V from TestClass1. The test showed 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. 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!


Static Methods in php

Self: indicates that the static method get () is called in this class. Because the static method can only be called in the static method, the instance method cannot be called, if you call the instance method, use this,

Php static method call problems

Currently, PHP can be used in both methods. Static classes are only convenient to call and do not need to be initialized.
But if your statement is incorrect. Static classes lose the static call capability.
For example.

<? Php
Class Cat {
}
Class Hypnotic_Cat extends Cat
{
Public $ string = "The cat was hypnotized. \ r \ n ";
Function Hypnotic_Cat ()
{
}
Public static function hypnotize ()
{
Echo self: $ string;
}
}
Hypnotic_Cat: hypnotize ();
$ Hypnotic_cat = new Hypnotic_Cat ();
$ Hypnotic_cat-> hypnotize ();
?>

Correct example

<? Php
Class Cat {
}
Class Hypnotic_Cat extends Cat
{
Public static $ string = "The cat was hypnotized. \ r \ n ";
Function Hypnotic_Cat ()
{
}
Public static function hypnotize ()
{
Echo self: $ string;
}
}
Hypnotic_Cat: hypnotize ();
$ Hypnotic_cat = new Hypnotic_Cat ();
$ Hypnotic_cat-> hypnotize ();
?>

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.