2012-09-14 40 views
0

当我在A类做一个查询,我把所有的数据了。 B类需要使用一些数据。我更喜欢查询结果的通部分到B比做B. B类新的查询将执行一些工作,数据将在B类改变如何传递给B类数组$ something_else?这里是类:通查询产生的另一个类

class A{ 
    public $something; 
    private $_project_obj; 
    function __construct($id = null){ 
    if ($id) { 
     $this->id = $id; 
     $this->populate($this->id); 
    } 
} 
function populate(){ 
     $query = //do query 
     $this->somthing= $query['A']; 
     $this->something_else = $query['B']; 
} 
function save(){ 
    // call save() in class B, $something_else is saved there 
    if ($this->_project_obj instanceof B) { 
    if (true !== $this->_project_obj->save()) { 
     return false; 
    } 
    } 
    // save $something and other stuffs in class A 
    // ...... 
    } 
    function project() { 
    if (!$this->_project_obj instanceof B) { 
    if (($this->id) && (loggedin_user_id())) { 
     $this->_project_obj = new B($this->id, loggedin_user_id()); 
    } else { 
    return false; 
    } 
    } 
    return $this->_project_obj 
    } 
} 
class B{ 
    public $data_this; 
    public $data_that; 
    function __constructor($id=null, $user_id=null){ 
     if($id && $user_id){ 
     return $this->populate(); 
     } 
     return true; 

    } 
function populate(){ 
    $query = // do the same query as in class A 
    $something_else = $query['B']; 
    $this->data_this = $something_else['a']; 
    $this->data_that = $something_else['b']; 
} 
function save(){ 
    // save all data as $something_else 
} 
function jobs(){ 
// perform jobs 
} 
} 
+1

“如何传递数组$ something_else”...什么?目前还不清楚这里需要发生什么。 – MidnightLightning

+0

至B类。除B类需要数据外,其余需要发生。 – Jenny

回答

0

目前尚不清楚其中在你需要something_else B,所以我们只添加它作为构造函数的一部分:使构造函数接受something_else一个额外的参数并将其保存到该类的属性:

class B{ 
    private var $_parent; 
    function __constructor($parent, $id=null, $user_id=null){ 
     $this->_parent = $parent; // Save reference to the "A" that contains this "B" 
     if($id && $user_id){ 
     return $this->populate(); 
     } 
     return true; 

    } 

当A创建了一个B:$this->_project_obj = new B($this, $this->id, loggedin_user_id());

而当B需要从其父母得到something_else的最新版本答:$this->_parent->something_else

+0

可能这是我的解决方案。请你帮我检查一下我的代码,看看 - 如果使用这种解决方案,当工作在B班中完成并保存时,B班是否仍然使用从A班收到的旧$ something_else?执行作业后,我需要更新$ something_else。 – Jenny

+0

@Jenny你还没有发布任何代码来说明'B'如何执行作业,所以我没有办法告诉你它是否正确地使用'$ something_else'。总的来说,我不明白为什么你们有两个班级,因为它似乎重复了许多类似的功能和复杂的事情。 – MidnightLightning

+0

A类是创建内容的对象,B类是处理用户关于这篇文章的各种设置的对象。这两个类都工作在相同的post_id。但他们的工作不会相互影响。 – Jenny

0
class class_b 
{ 

public $something_else = NULL 

} 

$a = new class_a(); 
$b = new class_b(); 

$b->something_else = $a->something_else 
+0

我应该在哪里写下$ b?如果不在A类内,我仍然会做一个新的查询? – Jenny

+0

'$ b'是你班级的实例。因此,而不是'$ b',使用你在执行你的东西时所做的任何实例。 – 2012-09-14 04:13:18

+0

$ something_else必须从数据库查询或从A类传递。这是我需要解决的关系。 – Jenny