PHP static properties and calls to static methods

Source: Internet
Author: User

In this paper, we analyze the static static properties of the PHP object-oriented system and the call to it. It is easy to understand how they are called (whether they can be invoked or how they are invoked), and they need to understand where they are stored in memory. Static properties, methods (both static and non-static) in memory, only one location (not static properties, how many instantiated objects, there are many attributes).

Instance:

The code is as follows Copy Code

<?php
Header ("Content-type:text/html;charset=utf-8");
Class human{
static public $name = "little Sister";
Public $height = 180;
static public function tell () {
echo self:: $name;//static method calls the static property, using the Self keyword
echo $this->height;//wrong. Static methods cannot call Non-static properties
Because $this represents an instantiated object, and this is a class, I don't know which object $this represents
}
Public function say () {
echo Self:: $name. "I spoke";
Normal method calls the static property, also uses the Self keyword
Echo $this->height;
}
}
$p 1 = new Human ();
$p 1->say ();
$p 1->tell ()//object can access static methods
echo $p 1:: $name;//object access static properties. You can't visit $p1->name like that.
Because the memory location of the static property is not in the object
Human::say ()//wrong. Say () method has $this error;
But php5.4 above will prompt
?>

Static properties

A static property means that its value retains its value, such as n objects instantiated in a class, so you can define a static property in the constructor to remember the number of objects. A static property in a class is about the same as a static variable, but there seems to be a more restrictive use in the class. Let's take a look at the general variables:

The code is as follows Copy Code

1.<?php
2.function Test () {
3. $n = 1;
4. Echo "The number is: $n <br/>";
5. $n + +;
6.}
7.test ();
8.test ();
9.test ();
10.test ();
11.test ();
12.test ();
13.?>

Obviously the result of this function is as follows:

The number is:1

The number is:1

The number is:1

The number is:1

The number is:1

The number is:1

But if your program is like this:

The code is as follows Copy Code
1.<?php
2.function Test () {
3. Static $n = 1;
4. Echo "The number is: $n <br/>";
5. $n + +;
6.}
7.test ();
8.test ();
9.test ();
10.test ();
11.test ();
12.test ();
13.?>

We just add a static keyword to the variable name, and the result is vastly different:

The number is:1

The number Is:2

The number Is:3

The number Is:4

The number Is:5

The number Is:6

The 1.static keyword can be used to modify variables, methods (static methods)

2. Without instantiating, you can directly access static properties and static methods in a class.

3.static properties and methods, you can access static properties and methods only, and you cannot access non-static properties and methods. Because static properties and methods are created, there may not be any instances of this class that can be invoked.

4. If you want to access static members in the current class, you can use the self:: keyword for access.

5. We cannot use this key to access static properties in the class because static properties already exist before the object may have been instantiated.

6. Static methods access static properties in the class, using the class name:: Static property names can call static properties in a class.


static method

  code is as follows copy code
1.<?php  
2.class test {  
3. private static $money = 2000;  
4. public static function Getonemon () {  
5.  return test:: $money;  &n Bsp
6.}  
7. public static function Gettwomon () {  
8.  self:: $money = self:: $mon ey-1500;  
9.  return self:: $money;  
10.}   
11.}   
12.echo "I now balance is:";  
13.echo Test:: Getonemon ();  
14.echo "<br>";   
15.echo "After consumption, my balance is:";  
16.echo Test:: Gettwomon ();  
17.? > 


In this example, we see two ways to access the value of a static property $money: One is the class name previously mentioned:: The form of a property value, and the other is the use of the Self keyword. Of course it is recommended to use the Self keyword in this way, because if the day is not happy, we modify the class name, so if you use the first way, do you still have to revise down the method of using it, of course you have to be in the same class, if you are in the subclass to call the parent class static properties and methods, Then you have to use the parent:: the way.

Say it again.

1: If you want to call other static methods in the static method, use the method is: Class Name:: Method name in the form of the call, or the sentence, if you are in the same class for such a call, use the SELFT keyword to invoke it. Otherwise, your program will have to make an error.

Static methods in 2:php cannot invoke non-static properties and Non-static methods, nor can they use the class name:: Or self:: Calling Non-static properties. You cannot use the $this-> property name to invoke, in short, static properties or methods can only be invoked in a static method, and non-static cannot be invoked.


Conclusion:

(1), static properties can be invoked without instantiation. Because the position of the static property is in the class, the method is called "Class Name:: Property name";
(2), static methods can be invoked without instantiation. Ditto
(3), static methods cannot invoke Non-static properties. Because non-static attributes need to be instantiated, they are stored in the object.
(4), static methods can call Non-static methods, using the Self keyword. In PHP, a method is self:: After that, it automatically turns into a static method; (prompts strict)

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.