English 中文(简体)
混杂: 在我的类数据成员内部的数组内, 有无穷无尽的阵列。 分类设计不好吗?
原标题:Confusion: Endless arrays within arrays inside my class data members. Bad class design?

假设有三种等级:

Sponsor id <[int], name [string], 赞助的项目 [字符串], 赞助的项目 [数组 Project 对象]}

project > id [int], name [string], desc [string], succors <[强势>Sponsor 对象的数组]}

" 强 " 学校 < /强'学校 < /强'code > id < /code > [int], name [string], projectsDone [强 " 强 " 项目

我被困在 < strong > sponsor s < code > 赞助的项目 和 < strong > Project s < code > 赞助者 之间。

从理论上讲,如果我创建一个“强”项目 < / 强”, 那么我也需要为此创建“ 强” 赞助人 < / 强” 。 如果我创建了“强” 赞助人 < / 强”, 那么我也需要为此创建“强” 项目 < / 强” 。

因此,这会造成无穷无尽的 " 强 " 项目

如果是这样的话,我该如何避免这一缺陷?

我的目标是从返回的多个 SQL 结果中临时创建所有这些对象, 然后再将它发送到 JavaScript 页面 。

final class Sponsor{
    private $_id;
    private $_name;
    private $_projects;

    public function __construct($id, $name, Project $projects){
        $this->setSponsor($id, $name, $projects);
    }

    public function getId(){
        return $this->_id;
    }

    public function getName(){
        return $this->_name;
    }

    public function getProjects(){
        return $this->_projects;
    }

    private function setId($id){
        $this->_id = $id;
    }

    private function setName($name){
        $this->_name = $name;
    }

    private function setProject(Project $projects){
        $this->_projects = array();
        foreach($projects as $val){
            array_push($this->_projects, $val);
        }
    }

    private function setSponsor($id, $name, Project $projects){
        $this->setId($id);
        $this->setName($name);
        $this->setProjects($projects);
    }
}

final class Project{
    private $_id;
    private $_name;
    private $_desc;
    private $_sponsors;

    public function __construct($id, $name, $desc, Sponsor $sponsors){
        $this->setProject($id, $name, $desc, $sponsors);
    }

    private function setId($id){
        $this->_id = $id;
    }

    private function setName($name){
        $this->_name = $name;
    }

    private function setDesc($desc){
        $this->_desc = $desc;
    }

    private function setSponsors(Sponsor $sponsors){
        $this->_sponsors = array();
        foreach($sponsors as $obj){
            $id = $obj->getId();
            $name = $obj->getName();
            $projects = $obj->getProjects();
            array_push($this->_sponsors, new Sponsor($id, $name, Project $this));
        }
    }

    private function setProject($id, $name, $desc, Sponsor $sponsors){
        $this->setId($id);
        $this->setName($name);
        $this->setDesc($desc);
        $this->setSponsors($sponsors);
    }
}

final class School{

    private $_id;
    private $_name; 
    private $_projectsDone;

    public function __construct($id, $name, Project $projects){
        $this->setSchool($id, $name, $projects);
    }

    private function setId($id){
        $this->_id = $id;
    }

    private function setName($name){
        $this->_name = $name;
    }

    private function setProjectsDone($projects){
        $this->_projectsDone = array();
        foreach($projectsDone as $val){
            $this->{_projectsDone}[] = $val;
        }
    }

    public function setSchool($id, $name, $projectsDone){
        $this->setId($id);
        $this->setName($name);
        $this->setProjectsDone($projectsDone);
    }
}
最佳回答

您已经有了一个 < a href= > 的问题。 “ http:// en.wikipedia. org/ wiki/ circulal_ reference" rel= “ nofollow” > circural reference 。 每一个对象都持有与其相关的物体阵列, 尝试保留其代号的阵列, 并在需要时查找相关实体 。

问题回答

物体相互之间互相保持联系是没有错的。 它非常常见, 并且是一种正常( 弹性) 的关系。 比如, 标注中的 DOM. 元素节点知道自己的孩子, 其孩子知道自己的父母 。

显然,在建造和初始化它们时,你需要小心小心, 因为有一点点鸡和鸡amp; 鸡蛋问题, 但支票归结到

if relation to object not established
    establish()

您提到过您将此信息存储在数据库中。 您可能甚至可以避免此检查。 例如, 您可能拥有以下表格 :

Sponsor
Project
School
SponsoredProjects - many to many relation
SchoolProjectsDone - many to many relation

构造赞助者、项目和学校对象,然后在赞助项目和学校项目表格中环绕关系,将对象联系起来。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签