Implements the code _php techniques for a PHP5 Getter/setter base class

Source: Internet
Author: User
Tags mixed
PHP3 and PHP4 all have classes, but their class definitions are really bad and inefficient, but the data says that PHP5 reconstructs object-oriented support, though not entirely object-oriented, but can be seen.
Last night, idle and bored to get this thing, feel PHP5 added class member permissions keyword is good, but the problem again, it seems that there is no convenient way to define the field getter and setter, the traditional way is defined:

Class A
{
Private $field;
Public Function Get_field () {return $this-> $field;}
Public Function Set_field ($value) {$this->field = $value;}
}

It's easy to implement, but to be honest, it sucks to write a bunch of code for a field.
So I wondered if there was a more convenient way to solve it, and it was easy to define its type limitations.
After a long time (no way, not familiar with it.) ) and finally a class to solve the 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 property 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 ("This value type is incorrect, 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 is not allowed to be null value");
}
}
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 ("This value type is incorrect, 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 ("This value type is incorrect, must be". $type. "Type");
}
}
else if (Is_null ($value) && $allow _null)
{
$this->fields[$key] ["value"] = NULL;
}
}
}
Else
{
throw new Exception ("This property does not exist");
}
}
}

It is easier to define the type of the field, to allow null values, and to default values by defining a certain format array.

The test code is as follows:

Class Test extends Abstract_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);

Here to implement the getter and setter, but due to the time relationship I did not implement ReadOnly function, in fact, is very simple, is to add an item, logo it can not be rewritten as

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.