Differences between static and static variables in PHP

Source: Internet
Author: User
Document directory
  • If the declared class member or method is static, you can directly access the static member without instantiating the class. You cannot access the static member through an object (except for static methods, the method can be ). Static members belong to the class and do not belong to any object instance, but the object instances of the class can be shared.
If the declared class member or method is static, you can directly access the static member without instantiating the class. You cannot access the static member through an object (except for static methods, the method can be ). Static members belong to the class and do not belong to any object instance, but the object instances of the class can be shared. <? PHP
Class person {
// Define static member attributes
Public static $ Country = "China ";
// Define the static member Method
Public static function mycountry (){
// Static member attributes for internal access
Echo "I am". SELF: $ country. "person <br/> ";
}
}
Class student extends person {
Function Study (){
Echo "I am". Parent: $ country. "person <br/> ";
}
}
// Output the member property value
Echo person: $ country. "<br/>"; // output: China
$ P1 = new person ();
// Echo $ P1-> country; // incorrect syntax
// Method for accessing static members
Person: mycountry (); // output: I am a Chinese
// Static methods can also be accessed through objects:
$ P1-> mycountry (); // The member attribute value is output in the subclass.
Echo Student: $ country. "<br/>"; // output: China
$ T1 = new student ();
$ T1-> Study (); // output: I am a Chinese
?>
 

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.