douzhan5262 2012-07-20 02:22
浏览 32
已采纳

使用PHP中的方法链接的OOP

First of all - my English is bad so I hope you will understand. I have question involving method chaining on nested objects structure.

I'm trying to create some kind of a list with objects and sub lists that create itself on the fly. So far i have this code:

interface IData {}


class root {
    public function __construct( root $proxy = null ) {
        $this->proxy = $proxy;
    }
    public function __destruct() {
        if( !is_null( $this->proxy ) ) {
            unset( $this->proxy );
        }
    }
    public function OpenList() {
        $list = new field_list( $this );
        $this->data[] = $list;
        return $list;
    }

    public function CloseList() {
        return $this->proxy;
    }

    public function Add() {
        $this->data[] = new field();
        return $this;
    }


    private $proxy = null;
    private $data = array();
}

class field_list extends root implements IData {
    public function __construct( root $proxy ) {
        parent::__construct( $proxy );
    }
    public function __destruct() {
         parent::__destruct();
    }
}

class field implements IData {}

I'm using my code as follows:

$root = new root();
$root->OpenList()->Add()->CloseList()->OpenList()->Add()->Add()->Add()->OpenList()->Add();

I can append to $root field objects (class field), but also open new lists (class field_list) and append field objects to the last opened list.

I wonder if the object reference i created ($proxy) will cause memory leak problems, and also if there are any performance issues with this code. (EDIT:) when i print_r($root); I'm getting recursion in the output, and I don't know if it's some type of a problem with my objects structure.

root Object(
[proxy:root:private] => 
[data:root:private] => Array
    (
        [0] => field_list Object
            (
                [proxy:root:private] => root Object
  *RECURSION*
                [data:root:private] => Array
                    (
                        [0] => field Object
                            (
                            )

                    )

            )

        [1] => field_list Object
            (
                [proxy:root:private] => root Object
  *RECURSION*
                [data:root:private] => Array
                    (
                       ........
                    )

            )

    )

)

Thank you

  • 写回答

1条回答 默认 最新

  • duandi5328 2012-07-20 04:32
    关注

    I've seen chained method calls before, and I don't think that I've ever encountered any issues. In terms of memory leaks, I doubt there is a problem because unlike in languages like C++ or Java, you don't have to deal with the memory. You've taken care to write a destructor, so I doubt there will be a problem.

    Since you are creating only 1 instance, it should only refer to 1 instance of the property $proxy, so I'm thinking this could be OK.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么