PHP static _php Tutorials with a few caveats to using static methods

Source: Internet
Author: User

PHP static methods with a few caveats, PHP


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

This code is what the output would be, I thought it would be normal_v from TestClass1
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!


Problems with static methods in PHP

Self:: Is the delegate calls the static method get () in this class, because in static method can only adjust static method, cannot call instance method, if call instance method, then use this,

static method call problem for PHP

php currently both methods can be used, static class, just say convenient call, do not initialize class.
But if you're wrong. Static classes lose the ability to call statically.
Give an example of the error.

!--? php
class cat {
}
Class Hypnotic_cat extends cat
{
Public $string = "The Cat is Hypnot Ized.\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 ();
.
 

http://www.bkjia.com/PHPjc/879726.html www.bkjia.com true http://www.bkjia.com/PHPjc/879726.html techarticle several considerations for using static methods in PHP, PHP static This article introduces several common considerations for using static methods in PHP. Share to everyone for your reference. The specific method is as follows: ...

  • Related Article

    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.