Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数据库表创建对象_Php_Oop - Fatal编程技术网

Php 从数据库表创建对象

Php 从数据库表创建对象,php,oop,Php,Oop,示例数据库: level1 | level2 | level3 | datatype | maxlen | minlen ----------------------------------------------------------- version | NULL | NULL | element | 0 | 0 version | release | NULL | string | 2 | 0 version | b

示例数据库:

level1  | level2      | level3 | datatype | maxlen | minlen
-----------------------------------------------------------
version | NULL        | NULL   | element  | 0      | 0
version | release     | NULL   | string   | 2      | 0  
version | build       | NULL   | string   | 5      | 0
service | NULL        | NULL   | element  | 0      | 0
service | subservice1 | NULL   | element  | 0      | 0
service | subservice1 | value1 | string   | 10     | 5
service | subservice1 | value2 | string   | 50     | 1
这个表(我在这个示例中省略了一组)表示SOAP API的默认值(字符串长度、必需/可选标志和其他内容)。 我试过几种方法,但没有一种能把我带到我想去的地方。我想我不太理解递归性的概念(这是一个词吗?)

我需要的是一个物体

stdClass Object (
    [version] => stdClass Object (
        [release] => stdClass Object (
            [maxlen] => 2
            [minlen] => 0
        )
        [build] => stdClass Object (
            [maxlen] => 5
            [minlen] => 0
        )       
    )
    [service] => stdClass Object (
        [subservice1] => stdClass Object (
            [value1] => stdClass Object (
                [maxlen] => 10
                [minlen] => 5
            )
            [value2] => stdClass Object (
                [maxlen] => 50
                [minlen] => 1
            )
        )
    )
)
等等。有多达7个级别,我希望能够访问这样的默认值

$var = $object->service->subservice1->value1->maxlen

也许有人能给我指出正确的方向?谢谢大家!

我一点也不喜欢,但它似乎很管用

CLASS dhl_object {

        public $objTemplate;


        public function __construct() {

            global $conn_secuser;

            if(!$result = getAllRows($conn_secuser, "reference_dhl")) {
                return FALSE;
            } else {

                if($result->num_rows<=0) {
                    return FALSE;
                } else {

                    $object = new stdClass();

                    while($row = $result->fetch_assoc()) {

                        $id     =   $row["id"];
                        $L1     =   $row["L1"];
                        $L2     =   $row["L2"];
                        $L3     =   $row["L3"];
                        $L4     =   $row["L4"];
                        $L5     =   $row["L5"];
                        $L6     =   $row["L6"];
                        $L7     =   $row["L7"];
                        $dataType   =   $row["dataType"];
                        $minLength  =   $row["minLength"];
                        $maxLength  =   $row["maxLength"];
                        $required   =   $row["required"];
                        $note       =   $row["note"];

                        // possible dataTypes: Element, String, Decimal, Integer, Enumeration
                        if($dataType!="Element") {

                            if($L7) {

                                if(!isset($object->$L1)) $object->$L1 = new stdClass();
                                if(!isset($object->$L1->$L2)) $object->$L1->$L2 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3)) $object->$L1->$L2->$L3 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4)) $object->$L1->$L2->$L3->$L4 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4->$L5)) $object->$L1->$L2->$L3->$L4->$L5 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4->$L5->$L6)) $object->$L1->$L2->$L3->$L4->$L5->$L6 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4->$L5->$L6->$L7)) $object->$L1->$L2->$L3->$L4->$L5->$L6->$L7 = new stdClass();

                                $object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->minLength   = $minLength;
                                $object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->maxLength   = $maxLength;
                                $object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->required    = $required;
                                $object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->note        = $note;
                                $object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->value       = "";

                            } else
                            if($L6) {

                                if(!isset($object->$L1)) $object->$L1 = new stdClass();
                                if(!isset($object->$L1->$L2)) $object->$L1->$L2 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3)) $object->$L1->$L2->$L3 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4)) $object->$L1->$L2->$L3->$L4 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4->$L5)) $object->$L1->$L2->$L3->$L4->$L5 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4->$L5->$L6)) $object->$L1->$L2->$L3->$L4->$L5->$L6 = new stdClass();

                                $object->$L1->$L2->$L3->$L4->$L5->$L6->minLength    = $minLength;
                                $object->$L1->$L2->$L3->$L4->$L5->$L6->maxLength    = $maxLength;
                                $object->$L1->$L2->$L3->$L4->$L5->$L6->required     = $required;
                                $object->$L1->$L2->$L3->$L4->$L5->$L6->note         = $note;
                                $object->$L1->$L2->$L3->$L4->$L5->$L6->value        = "";

                            } else
                            if($L5) {

                                if(!isset($object->$L1)) $object->$L1 = new stdClass();
                                if(!isset($object->$L1->$L2)) $object->$L1->$L2 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3)) $object->$L1->$L2->$L3 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4)) $object->$L1->$L2->$L3->$L4 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4->$L5)) $object->$L1->$L2->$L3->$L4->$L5 = new stdClass();

                                $object->$L1->$L2->$L3->$L4->$L5->minLength = $minLength;
                                $object->$L1->$L2->$L3->$L4->$L5->maxLength = $maxLength;
                                $object->$L1->$L2->$L3->$L4->$L5->required  = $required;
                                $object->$L1->$L2->$L3->$L4->$L5->note      = $note;
                                $object->$L1->$L2->$L3->$L4->$L5->value     = "";

                            } else
                            if($L4) {

                                if(!isset($object->$L1)) $object->$L1 = new stdClass();
                                if(!isset($object->$L1->$L2)) $object->$L1->$L2 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3)) $object->$L1->$L2->$L3 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3->$L4)) $object->$L1->$L2->$L3->$L4 = new stdClass();

                                $object->$L1->$L2->$L3->$L4->minLength  = $minLength;
                                $object->$L1->$L2->$L3->$L4->maxLength  = $maxLength;
                                $object->$L1->$L2->$L3->$L4->required   = $required;
                                $object->$L1->$L2->$L3->$L4->note       = $note;
                                $object->$L1->$L2->$L3->$L4->value      = "";

                            } else
                            if($L3) {

                                if(!isset($object->$L1)) $object->$L1 = new stdClass();
                                if(!isset($object->$L1->$L2)) $object->$L1->$L2 = new stdClass();
                                if(!isset($object->$L1->$L2->$L3)) $object->$L1->$L2->$L3 = new stdClass();

                                $object->$L1->$L2->$L3->minLength   = $minLength;
                                $object->$L1->$L2->$L3->maxLength   = $maxLength;
                                $object->$L1->$L2->$L3->required    = $required;
                                $object->$L1->$L2->$L3->note        = $note;
                                $object->$L1->$L2->$L3->value       = "";

                            } else
                            if($L2) {

                                if(!isset($object->$L1)) $object->$L1 = new stdClass();
                                if(!isset($object->$L1->$L2)) $object->$L1->$L2 = new stdClass();

                                $object->$L1->$L2->minLength    = $minLength;
                                $object->$L1->$L2->maxLength    = $maxLength;
                                $object->$L1->$L2->required     = $required;
                                $object->$L1->$L2->note         = $note;
                                $object->$L1->$L2->value        = "";

                            } else
                            if($L1) {

                                if(!isset($object->$L1)) $object->$L1 = new stdClass();

                                $object->$L1->minLength = $minLength;
                                $object->$L1->maxLength = $maxLength;
                                $object->$L1->required  = $required;
                                $object->$L1->note      = $note;
                                $object->$L1->value     = "";

                            } 

                        }

                    }

                    $this->objTemplate = $object;

                }
            }

        }

    }
类dhl_对象{
公共$objTemplate;
公共函数构造(){
全球$conn_secuser;
if(!$result=getAllRows($conn\u secuser,“reference\u dhl”)){
返回FALSE;
}否则{
如果($result->num\u rowsfetch\u assoc()){
$id=$row[“id”];
$L1=$row[“L1”];
$L2=$row[“L2”];
$L3=$row[“L3”];
$L4=$row[“L4”];
$L5=$row[“L5”];
$L6=$row[“L6”];
$L7=$row[“L7”];
$dataType=$row[“dataType”];
$minLength=$row[“minLength”];
$maxLength=$row[“maxLength”];
$required=$row[“required”];
$note=$row[“note”];
//可能的数据类型:元素、字符串、十进制、整数、枚举
if($dataType!=“Element”){
如果有的话(17美元){
如果(!isset($object->$L1))$object->$L1=新stdClass();
如果(!isset($object->$L1->$L2))$object->$L1->$L2=新stdClass();
如果(!isset($object->$L1->$L2->$L3))$object->$L1->$L2->$L3=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4))$object->$L1->$L2->$L3->$L4=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4->$L5))$object->$L1->$L2->$L3->$L4->$L5=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4->$L5->$L6))$object->$L1->$L2->$L3->$L4->$L5->$L6=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4->$L5->$L6->$L7))$object->$L1->$L2->$L3->$L4->$L5->$L6->$L7=新stdClass();
$object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->minLength=$minLength;
$object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->maxLength=$maxLength;
$object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->required=$required;
$object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->note=$note;
$object->$L1->$L2->$L3->$L4->$L5->$L6->$L7->value=“”;
}否则
如果有的话(16美元){
如果(!isset($object->$L1))$object->$L1=新stdClass();
如果(!isset($object->$L1->$L2))$object->$L1->$L2=新stdClass();
如果(!isset($object->$L1->$L2->$L3))$object->$L1->$L2->$L3=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4))$object->$L1->$L2->$L3->$L4=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4->$L5))$object->$L1->$L2->$L3->$L4->$L5=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4->$L5->$L6))$object->$L1->$L2->$L3->$L4->$L5->$L6=新stdClass();
$object->$L1->$L2->$L3->$L4->$L5->$L6->minLength=$minLength;
$object->$L1->$L2->$L3->$L4->$L5->$L6->$maxLength=$maxLength;
$object->$L1->$L2->$L3->$L4->$L5->$L6->required=$required;
$object->$L1->$L2->$L3->$L4->$L5->$L6->note=$note;
$object->$L1->$L2->$L3->$L4->$L5->$L6->value=“”;
}否则
如果有的话(15美元){
如果(!isset($object->$L1))$object->$L1=新stdClass();
如果(!isset($object->$L1->$L2))$object->$L1->$L2=新stdClass();
如果(!isset($object->$L1->$L2->$L3))$object->$L1->$L2->$L3=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4))$object->$L1->$L2->$L3->$L4=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4->$L5))$object->$L1->$L2->$L3->$L4->$L5=新stdClass();
$object->$L1->$L2->$L3->$L4->$L5->minLength=$minLength;
$object->$L1->$L2->$L3->$L4->$L5->maxLength=$maxLength;
$object->$L1->$L2->$L3->$L4->$L5->required=$required;
$object->$L1->$L2->$L3->$L4->$L5->note=$note;
$object->$L1->$L2->$L3->$L4->$L5->value=“”;
}否则
如果有的话(L4美元){
如果(!isset($object->$L1))$object->$L1=新stdClass();
如果(!isset($object->$L1->$L2))$object->$L1->$L2=新stdClass();
如果(!isset($object->$L1->$L2->$L3))$object->$L1->$L2->$L3=新stdClass();
如果(!isset($object->$L1->$L2->$L3->$L4))$object->$L1->$L2->$L3->$L4=新stdClass();
$object->$L1->$L2->$L3->$L4->minLength=$minLength;
$object->$L1->$L2->$L3->$L4->maxLength
$root = (object) [];

while($row = $result->fetch_assoc()) {
    if ($row['datatype'] == 'Element') {
        continue;
    }

    $tgt = &$root;
    for($i = 1; $i <= 7; ++$i) {
        $rowkey = 'L'.$i;

        if ($row[$rowkey] === null) {
            break;
        }

        if (!property_exists($tgt, $row[$rowkey])) {
            $tgt->{$row[$rowkey]} = (object) [];
        }

        $tgt = &$tgt->{$row[$rowkey]};
    }

    $tgt = (object) [
        'minLength' => $row['minLength'],
        'maxLength' => $row['maxLength'],
        'required'  => $row['required'],
        'note'      => $row['note'],
        'value'     => ''
    ];
}
unset($tgt);