Implements a PHP5 Getter/setter base class Code _php Tutorial

Source: Internet
Author: User
PHP3, PHP4 have classes, but their class definition is very ugly, the efficiency is very embarrassing, but the data said PHP5 to reconstruct the object-oriented support, although not completely object-oriented, but also can be brought out to see people.
Last night idle bored then get up this thing, feel PHP5 added class member permissions keyword is very good, but the problem comes again, there seems to be no convenient way to define the field getter and setter, the traditional way is defined as:

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

Although it's easy to implement, it's really annoying to write this heap of code for a field.
So it is thought that there is a more convenient way to solve it, and it is easy to define its type restrictions or something.
A half-day (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" and "" ",
"Resource" and "" ",
"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, 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 ("The value is not allowed to be a 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 ("The 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 ("The 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 attribute does not exist");
}
}
}

Defining an array of a certain format makes it easier to define the type of the field, whether null values are allowed, and the default values.

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 side implements getter and setter, but because of the time relationship I did not go to achieve readonly function, in fact, is very simple, is to add an item, logo it can not be rewritten as

http://www.bkjia.com/PHPjc/317883.html www.bkjia.com true http://www.bkjia.com/PHPjc/317883.html techarticle PHP3, PHP4 have classes, but their class definition is very ugly, the efficiency is very embarrassing, but the data said PHP5 to reconstruct the object-oriented support, although not completely oriented ...

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