2012-11-12 115 views
0

我是比較新的PHP和有一些體面的成功,但我遇到了這個問題:創建新實例失敗PHP

如果我試圖創建類GenericEntryVO的新實例,我得到一個500錯誤與幾乎沒有有用的錯誤信息。但是,如果我使用通用對象作爲結果,則不會出現錯誤。我希望能夠將此對象作爲GenericEntryVO進行強制轉換,因爲我正在使用AMFPHP將序列化數據與Flex客戶端進行通信。

我讀過一些不同的方法來創建PHP但對於一個類Foo典型的「公共函數foo()」的構造被推薦爲PHP 5.4.4

//in my EntryService.php class 
public function getEntryByID($id) 
{ 
    $link = mysqli_connect("localhost", "root", "root", "BabyTrackingAppDB"); 

    if (mysqli_connect_errno()) 
    { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 

    $query = "SELECT * FROM Entries WHERE id = '$id' LIMIT 1"; 

    if ($result = mysqli_query($link, $query)) 
    { 
     // $entry = new GenericEntryVO(); this is where the problem lies! 

     while ($row = mysqli_fetch_row($result)) 
     { 
      $entry->id = $row[0]; 
      $entry->entryType = $row[1]; 
      $entry->title = $row[2]; 
      $entry->description = $row[3]; 
      $entry->value = $row[4]; 
      $entry->created = $row[5]; 
      $entry->updated = $row[6]; 
     } 
    } 

    mysqli_free_result($result); 
    mysqli_close($link); 

    return $entry; 
} 

//my GenericEntryVO.php class 
<?php 

class GenericEntryVO 
{ 
    public function __construct() 
    { 
    } 

    public $id; 
    public $title; 
    public $entryType; 
    public $description; 
    public $value; 


    public $created; 
    public $updated; 

    // public $properties; 
} 

?> 
再次
+3

請啓用錯誤記錄/讀取錯誤日誌以獲取「錯誤500」後面的真實錯誤消息。這應該引導你前進。 – eis

+0

你能分享一下這個「很少或沒有幫助的錯誤信息」嗎? –

+0

@eis它看起來很難找到GenericEntryVO.php文件?這很奇怪,因爲我在我的services.php文件中的其他方法使用GenericEntryVO類沒有問題。我也會粘貼它。 不能在這裏發表的代碼,因爲最大的字符,但這裏有引擎收錄鏈接: 錯誤日誌: [鏈接](http://pastie.org/5371978) 整個服務類: [鏈接]( http://pastie.org/5371989) – jusopi

回答

0

感謝鉛@eis。我沒有意識到你可以訪問PHP中的錯誤日誌。作爲一名as3/Flex開發人員,我習慣於使用帶有斷點的調試器。不確定PHP dev'rs是否有類似Flash Builder的IDE。

看到通用500錯誤是找到正確的類來實例化的問題後,我做了一些我自己的調查。我需要把require_once realpath(dirname(FILE)。'/ vo/GenericEntryVO.php');在班級的頂部。我使用的是as3語法,並希望文件路徑與正在處理的當前文件相關。這裏就是我發現這個解決方案:

How do I format a PHP include() absolute (rather than relative) path?這是關於第四或第五註釋下降一個名爲@Zoredache

我不知道這是否是最好的解決辦法的人,但有我並再次運行我的數據對象被序列化爲在Flex中消費。

+1

進行調試,看看xdebug以及如何將其與您最喜愛的IDE – fd8s0

+0

ooooh集成,將做謝謝@ fd8s0。他們爲Eclipse提供了大量的在線安裝資源。 – jusopi

+1

'realpath()'不是必需的。如果你使用php 5.3,你的例子就像'include_once __DIR __./ vo/GenericEntryVO.php';'一樣簡單。 – glasz