Implements the code of a PHP5 getter/setter base class.
Source: Internet
Author: User
The code for implementing a PHP5 gettersetter base class PHP3 and PHP4 all have classes, but their class definitions are really not decent and efficiency is quite difficult, however, in terms of materials, PHP5 re-constructs the object-oriented support. although it is not completely object-oriented, it can also be seen.
I started this last night when I was bored. I felt that the class member permission keyword added to PHP5 was quite good, but the problem came again, it seems that there is no convenient way to define the getter and setter of the field. the traditional method is defined as follows:
Class
{
Private $ field;
Public function get_field () {return $ this-> $ field ;}
Public function set_field ($ value) {$ this-> field = $ value ;}
}
Although it is easy to implement, it is really uncomfortable to write this pile of code for a field ..
So I thought about whether there is a more convenient way to solve it, and I can easily define its type restrictions or something.
After a long time (no way, not familiar with it ..), Finally, a class is created to solve this problem:
Parent: :__ construct ($ define );
}
}
$ A = new test ();
$ A-> id = 123;
Eche $ a-> id;
Echo $ a-> ins1;
$ A-> ins1 = NULL;
Echo is_null ($ a-> ins1 );
The getter and setter functions are implemented here, but I didn't implement the readonly function due to the time relationship. In fact, it is very easy to add one item to identify whether it can be rewritten.
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.