Implements the code of a PHP5 gettersetter base class. 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 not all PHP3 and PHP4 have classes, 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:
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.
...