2011-08-17 35 views
2

以下是我所做的:是否可以在php中將當前類傳遞給另一個類作爲參考?如果是,如何

假設兩個類一個事件,另一個GoogleCalendar。

class Event { 
    private $googleCalendar; 

    public function __construct() { 
    $this->googleCalendar = new GoogleCalendar(); 
    $this->googleCalendar->set_event($this); 
    } 
} 

class GoogleCalendar { 
    private $event; 

    public function set_event(&$event) { 
     $this->event = $event; 
    } 
} 

所以,當我訪問GoogleCalendar類的事件元素時,它說對象不存在。哪裏有問題 ?

提前致謝,讓我知道如果有什麼不清楚!

艾蒂安NOEL

+2

「當我訪問Google日曆類的事件元素」。你如何做到這一點? – Raveline

回答

1

對象是通過引用自動傳遞,你不需要&$event作爲參數。

下面是一個example

<?php 

    error_reporting(E_ALL); 

    class a 
    { 
     public $prop = 'test'; 
     function __construct() 
     { 
      $b = new b(); 
      $b->preform_action($this); 
     } 
    } 

    class b 
    { 
     public function preform_action($object) 
     { 
      if (is_object($object)) { 
       var_dump($object); 
      } 
     } 
    } 

    $a = new a(); 

?> 
+0

你知道它爲什麼說對象不存在嗎? – CoachNono

+0

非常感謝! – CoachNono

相關問題