Constant properties can only be accessed through a class and not through an instance of the class

Source: Internet
Author: User

1, static properties, static methods

In object-oriented programming, we can not only access methods and properties through objects, but also access them through classes. Such methods and properties are static and must be declared with the static keyword.

[PHP]View Plaincopyprint?
    1. Class Staticexample {
    2. Staticpublic $num = 0;
    3. Staticpublic function Saynum () {
    4. Echoself::num;
    5. }
    6. }

A static method is a function that takes a class as a scope. Static methods cannot access normal properties in this class, because those properties belong to an object, but static properties can be accessed. If you modify a static property, all instances of the class will have access to the new value.

Because static elements are accessed through classes instead of instances, accessing static elements does not require referencing the variables of the object, but instead uses:: (two colons) to concatenate the class name and property or class name and method.

[PHP]View Plaincopyprint?
    1. Echostaticexample::$num;
    2. Staticexample::saynum ();

To access a static method or property from the current class (not a subclass), you can use the Self keyword. Self points to the current class as if the pseudo-variable $this points to the current object. Therefore, you can use the class name to access the property $num outside of the Staticexample class:

[PHP]View Plaincopyprint?
    1. Staticexample::$num

Inside the Staticexample class, you can use the Self keyword

Use reason:

1. They are available anywhere in the code (assuming you can access the class), that is, you do not need to pass an instance of the class between objects, and you do not need to store the instance in a global variable to access methods in the class

2. Each instance of a class can access the static properties defined in the class, so you can use a static property to set the value that can be used by all objects of the class

3. You can access a static property or method without instantiating the object, so that we don't have to instantiate the object in order to get a simple function

2. Constant properties

Some properties cannot be changed.

In PHP 5 You can define a constant property in a class, and as with a global variable, a class constant cannot be changed once it is set. Constant attributes are declared with the Const keyword. Constants do not start with $ as regular properties do. By convention, you can only name constants in uppercase letters, as follows:

[PHP]View Plaincopyprint?
    1. Class Shopproduct {
    2. constavailable = 0;
    3. //...
    4. }

A constant property contains only the values of the base data type. You cannot assign an object to a constant. Like static properties, constant properties can only be accessed through the class and not through an instance of the class. You do not need to use $ as a leader when referencing constants, as follows:

[PHP]View Plaincopyprint?
    1. echoshopproduct::available;

Assigning a value to a constant that has already been declared causes a parsing error.

You should use constants when you need to have access to a property in all instances of the class, and if the property value does not need to be changed.

Constant properties can only be accessed through a class and not through an instance of the class

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.