Analysis of the usage of static class and static variables in PHP

Source: Internet
Author: User
Tags php class
This paper analyzes the difference between static class and static variable usage class in PHP, creates object $object = new Class () and then invokes with "--" $object->attribute/function, if the variable/method is accessible. Direct invocation of the class method amount: Class::attribute/function, either static/non-static, static: declares that a class member or method is static, can be accessed without instantiating a class, and cannot access static members through an object ( Except for static methods), static members belong to a class and do not belong to any object instance, but object instances of the class can be shared.

First download the class library for static and static variables used in PHP that we need to use in this lesson: http://www.php.cn/xiazai/leiku/610

After the download, find the PHP class files we need, unzip to our local directory, create a new PHP file!

Once this is done, we will invoke this class in the new PHP file, and instantiate the class:

<?phpinclude_once "person.php";//Import class file//Output member property value echo Person:: $country. " <br/> ";  Output: China $p1 = new Person ();//echo $p 1->country;   Error notation//Access static Member Method Person::mycountry ();   Output: I am Chinese//static methods can also be accessed by object: $p 1->mycountry ();//The Output member property value in the Subclass Echo Student:: $country. " <br/> "; Output: China $t1 = new Student (); $t 1->study ();    Output: I am a Chinese?>

Run the file and get the results as shown:

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.