Php object-oriented call of static attributes and static methods

Source: Internet
Author: User
: This article mainly introduces the call of static attributes and static methods in php object orientation. For more information about PHP tutorials, see. The static attributes and call of static methods in php object-oriented are analyzed here. Regarding their calls (whether they can be called or how to call them), you need to understand their location in the memory, which is very easy to understand. Static attributes and methods (including static and non-static attributes) have only one location in the memory (instead of static attributes, the attributes of how many instantiated objects are available ).

Instance:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

header("content-type:text/html;charset=utf-8");

classHuman{

staticpublic$name= "Xiaomei";

public$height= 180;

staticpublicfunctiontell(){

echoself::$name;// Call static attributes using the self keyword for static methods

// Echo $ this-> height; // error. Static methods cannot call non-static attributes.

// Because $ this represents the instantiated object, and this is a class, I don't know which object $ this represents

}

publicfunctionsay(){

echoself::$name. "I spoke";

// Call static attributes using common methods. the self keyword is also used.

echo$this->height;

}

}

$p1= newHuman();

$p1->say();

$p1->tell();// The object can access static methods

echo$p1::$name;// Static properties of object access. $ P1-> name cannot be accessed like this

// Because the memory location of the static attribute is not in the object

Human::say();// Error. An error occurs when the say () method $ this exists; when $ this exists, the result is returned.

// If php5.4 or above is used, a prompt is displayed.

?>

Conclusion:

1. static attributes can be called using self:, static or non-static methods;

2. static methods can call non-static methods and use the self keyword. In php, after a method is self:, it is automatically converted to a static method;

3. static methods cannot call non-static attributes. Because non-static attributes need to be instantiated and stored in objects;

The above describes the php object-oriented static attributes and static method calls, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.