Implements the code of a PHP5 getter/setter base class.

Source: Internet
Author: User

PHP3 and PHP4 all have classes, but their class definition is really not decent and efficiency is quite difficult. However, according to documents, PHP5 re-constructs object-oriented support, although it is not completely object-oriented, it can be used out.
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:

Class abstract_entity
{
Private $ fields;
Private $ sys_type = array (
"Bool" => "",
"Array" => "",
"Double" => "",
"Float" => "",
"Int" => "",
"Integer" => "",
"Long" => "",
"Null" => "",
"Object" => "",
"Real" => "",
"Resource" => "",
"String" => ""
// "Mixed" and "number"
);
Protected function _ construct ($ fields)
{
/*********************************\
* $ Fields = array (
* "Id" = array (
* "Allow_null" = false,
* "Value" = 1,
* "Type" = "int"
*);
*);
\**********************************/

$ This-> fields = $ fields;
}
Public function _ get ($ key)
{
If (array_key_exists ($ key, $ this-> fields ))
{
Return $ this-> fields [$ key] ["value"];
}
Else
{
Throw new Exception ("This attribute does not exist ");
}
}
Public function _ set ($ key, $ value)
{
If (array_key_exists ($ key, $ this-> fields ))
{
$ Allow_null = $ this-> fields [$ key] ["allow_null"];
$ Type = $ this-> fields [$ key] ["type"];
If (array_key_exists ($ type, $ this-> sys_type ))
{
$ Fun = create_function ('$ value', "return is _ $ type ($ value );");
If (@ $ fun ($ value ))
{
$ This-> fields [$ key] ["value"] = $ value;
}
Else if ($ allow_null & is_null ($ value ))
{
$ This-> fields [$ key] ["value"] = NULL;
}
Else
{
Throw new Exception ("the value type is incorrect and must be". $ type. "type ");
}
}
Else if ($ type = "mixed ")
{
If (! Is_null ($ value ))
{
$ This-> fields [$ key] ["value"] = $ value;
}
Else if ($ allow_null)
{
$ This-> fields [$ key] ["value"] = NULL;
}
Else
{
Throw new Exception ("this value cannot be NULL ");
}
}
Else if ($ type = "number ")
{
If (is_int ($ value) | is_float ($ value ))
{
$ This-> fields [$ key] ["value"] = $ value;
}
Else if (is_null ($ value) & $ allow_null)
{
$ This-> fields [$ key] ["value"] = NULL;
}
Else
{
Throw new Exception ("the value type is incorrect and must be". $ type. "type ");
}
}
Else
{
If (class_exists ($ type) | interface_exists ($ type ))
{
If (is_subclass_of ($ value, $ type ))
{
$ This-> fields [$ key] ["value"] = $ value;
}
Else if (is_null ($ value) & $ allow_null)
{
$ This-> fields [$ key] ["value"] = NULL;
}
Else
{
Throw new Exception ("the value type is incorrect and must be". $ type. "type ");
}
}
Else if (is_null ($ value) & $ allow_null)
{
$ This-> fields [$ key] ["value"] = NULL;
}
}
}
Else
{
Throw new Exception ("This attribute does not exist ");
}
}
}

By defining an array in a certain format, you can easily define the field type, whether to allow NULL values, and default values.

The test code is as follows:

Class test extends act_entity
{
Public function _ construct ()
{

$ Define = array (
"Id" => array (
"Allow_null" => false,
"Value" => 1,
"Type" => "int"
),
"Name" => array (
"Allow_null" => false,
"Value" => "abc ",
"Type" => "string"
),
"Gender" => array (
"Allow_null" => false,
"Value" => true,
"Type" => "bool"
),
"Ins" => array (
"Allow_null" => false,
"Value" => $ this,
"Type" => "test"
),

"Ins1" => array (
"Allow_null" => true,
"Value" => $ this,
"Type" => "test"
),
"Ins2" => array (
"Allow_null" => true,
"Value" => NULL,
"Type" => "config_media_type"
)
);

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.

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.