PHP would like to implement a field is assigned value can not be modified, so how to do it?

Source: Internet
Author: User
Tags php class
PHP itself does not have a modifier like C # readonly, which should be implemented by design.

Reply content:

PHP itself does not have a modifier like C # readonly, which should be implemented by design.

The constant is not OK?
Define (Key,value)

Talk is Cheap,i would show you the code demo.
Like this:

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

PHP does not provide such a function, but in object-oriented design, it can be implemented by 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, you can use the __set/__get two magic functions to achieve a better writing effect.

In PHP scripts can be implemented with define, in the PHP class can be implemented with the const

  • 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.