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 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义