Php cannot modify a field after it is assigned a value. how can this problem be implemented?

Source: Internet
Author: User
Php itself does not have modifiers similar to C # readonly. it should be implemented through design. php itself does not have modifiers similar to C # readonly. it should be implemented through design.

Reply content:

Php itself does not have modifiers similar to C # readonly. it should be implemented through design.

Isn't the constant enough?
Define (key, value)

Talk is cheap, I will show you the code demo.
Like this:

// 1. first snippetclass HelloSomeOne {const NAME = "PHP technology Daquan"; // todo something else .} // 2. second snippet // not in class body innerconst NAME = "PHP technology Daquan ";

Php does not provide such a function, but in the object-oriented design, it can be implemented through the set/get method.

class {    private $a = null;        public function setA($a) {        if (null === $this->a) {            $this->a = $a;        }    }    public function getA() {        return $this->a;    }}

For the set/get method, use the _ set/_ get magic functions to achieve better writing performance.

Define can be used in PHP scripts, and const can be used in PHP classes.

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.