The difference between self and static in PHP

Source: Internet
Author: User
Tags keyword list

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title> Page Title </title>
<meta name= "keywords" content= "keyword list"/>
<meta name= "description" content= "Web description"/>
<link rel= "stylesheet" type= "text/css" href= "/>
<style type= "Text/css" ></style>
<script type= "Text/javascript" ></script>
<body>
<?php
Class a{
static $p 1 = 1;
static function Showinfo () {//inheritable
Self represents the current class: The class in which the code resides (static binding)
echo "<br/>". Self:: $p 1;
}
static function ShowInfo2 () {//inheritable
Static also represents the "current class": The class that calls this method
The word "static" sometimes represents the same class as self,
There are also times when a different class is represented: static represents the caller (dynamic binding)
It is generally thought that this situation is more in line with the needs of reality
It can be seen that the self is completely replaced by static.
echo "<br/>".Static:: $p 1;
}
}
Class B extends a{
static $p 1 = 10;//Property Overrides
}
A::showinfo ();//No doubt: 1
B::showinfo ();/??? The result is 1.
Because self always represents the class of the code in which it resides
echo "A::showinfo2 ();//The result is 1.
B::showinfo2 ();//The result is 10.

?>
</body>

Results in the Web page output:

1
1


1
10

The difference between self and static in PHP

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.