Php object-oriented static property and static method call _ PHP Tutorial

Source: Internet
Author: User
Php targets the call of static attributes and static methods. Php object-oriented call of static attributes and static methods this article mainly introduces the call of static attributes and static methods in php object-oriented, the instance analyzes static attributes and calls of static methods in php object-oriented static attributes.

This article mainly introduces the call of static attributes and static methods in php object orientation. The example analyzes the principles and call skills of static attributes and static methods. For more information, see

This document describes how to call static attributes and methods in php. Share it with you for your reference. The details are as follows:

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 ");

Class Human {

Static public $ name = "Xiaomei ";

Public $ height = 180;

Static public function tell (){

Echo self: $ name; // static method call static attribute, using the self keyword

// 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

}

Public function say (){

Echo self: $ name. "I spoke ";

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

Echo $ this-> height;

}

}

$ P1 = new Human ();

$ P1-> say ();

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

Echo $ p1: $ name; // static attribute 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 without instantiation. Because static attributes are stored in the class, the calling method is "class name: Property Name ";
(2) static methods can be called without instantiation. Same as above
(3) static methods cannot call non-static attributes. Because non-static attributes need to be instantiated and stored in objects;
(4) 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;

I hope this article will help you with php programming.

This article describes the call of static attributes and static methods in php object orientation. the instance analyzes static attributes...

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.