PHP _php tutorial for static and static methods in object-oriented calls

Source: Internet
Author: User

Calls to static and static methods in object-oriented PHP


This article mainly describes the PHP object-oriented static property and static methods of the call, an example of static properties and static methods to analyze the principle and call skills, the need for friends can refer to the following

The examples in this article describe static static properties and static method calls in PHP. Share to everyone for your reference. Specific as follows:

In this paper, we analyze the static and static methods in the object-oriented PHP call. About their invocation (can be called, how to call), need to figure out where they are stored in memory, so it is very easy to understand. Static properties, methods (both static and non-static) in memory, only one location (not a static property, how many objects are instantiated, and how many properties).

Instance:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

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 call static property, use self keyword

echo $this->height;//wrong. Static methods cannot invoke non-static properties

Because $this represents the instantiation of an object, and here is the class, I don't know which object $this represent

}

Public function say () {

echo Self:: $name. "I have spoken";

The 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 property. You can't visit $p1->name like this.

Because the memory location of the static property is not in the object

Human::say ();//Wrong. Say () $this error when no $this is available

But php5.4 above will prompt

?>

Conclusion:

(1), static properties do not need to be instantiated to invoke. Because the static property is stored in a class, the calling method is "class name:: Attribute Name";
(2), static methods do not need to be instantiated to invoke. Ditto
(3), static methods cannot invoke non-static properties. Because the non-static attribute needs to be instantiated, it is 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 transforms into a static method;

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/955272.html www.bkjia.com true http://www.bkjia.com/PHPjc/955272.html techarticle the invocation of static and static methods in object-oriented PHP This article mainly introduces the static and static properties and methods in the object-oriented PHP, and analyzes the static properties ...

  • 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.