PHP Class Attribute and class static variable access method example, php example

Source: Internet
Author: User
Tags php example

PHP Class Attribute and class static variable access method example, php example

This article describes how to access class attributes and class static variables in PHP. We will share this with you for your reference. The details are as follows:

<? Php/* PHP class Attribute and class static variable access * Created on 2016-7-13 */class test {const constvar = 'Hello world'; static $ staticvar = 'Hello world '; function getStaticvar () {return self: $ staticvar ;}}$ obj = new test (); echo test: constvar; // output 'Hello world' echo @ test :: staticvar; // error. Before staticvar, you must add $ to access it. This is one of the areas that are easy to confuse with class constants (per-class constants). echo test: $ staticvar; // output 'Hello world' $ str = 'test'; // echo $ str: $ staticvar; // error. The class name cannot be dynamic here/ /Echo $ str: constvar; // the cause of the error is the same as above. // when a variable in the class name is in an uncertain (dynamic) state, the response class variable $ obj2 = new $ str (); echo $ obj2-> getStaticvar ();?>

Running result: hello world

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.