首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

第一個 Delphi for PHP 小程式:簡易加法練習與開發心得

     編輯:Delphi

花一小時寫了第一個 Delphi for PHP 程式:瀏覽器顯示簡單加法題,使用者輸入答案,系統判斷正誤並可繼續下一題。功能簡單,主要為熟悉環境,過程中查閱 PHP 說明,發現 CodeGear 的 PHP 說明做得不錯。

程式碼

// Includes
require_once("vcl/vcl.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

// Class definition
class Unit3 extends Page
{
  public $txtAnswer = null;
  public $btnB = null;
  public $btnA = null;
  public $btnRun = null;
  public $lblMsg = null;
  public $Button4 = null;
  public $Button2 = null;

  function btnRunClick($sender, $params)
  {
    $a = rand(1, 9);
    $b = rand(1, 9);
    $this->btnA->Caption = $a;
    $this->btnB->Caption = $b;
    $this->lblMsg->Caption = " ";
    $this->txtAnswer->Text = " ";
  }

  function txtAnswerSubmit($sender, $params)
  {
    global $a, $b, $c0, $c1;
    $a = $this->btnA->Caption;
    $b = $this->btnB->Caption;
    $c0 = $a + $b;
    $c1 = $this->txtAnswer->Text;
    if ($c0 == $c1) {
      $this->lblMsg->Caption = "Right";
    } else {
      $this->lblMsg->Caption = "Wrong";
    }
  }
}

global $application;
global $Unit3;

// Creates the form
$Unit3 = new Unit3($application);
// Read from resource file
$Unit3->loadResource(__FILE__);
// Shows the form
$Unit3->show();
?>

第一次寫 PHP 的感受

  1. Delphi for PHP IDE 強大、速度快,有 VCL for PHP 後 Zend IDE 可徹底忘記,流暢度僅 VS 可比。
  2. $this->lblMsg 這類物件寫法初上手不適應,看幾個範例後明白,其實與 Delphi 物件導向體系幾乎相同,只差語法。程式碼提示極快,若無提示通常是寫錯了。
  3. 寫 $this 很囉嗦,不知有無簡化方法。
  4. 面向物件僅需關注事件處理,上述程式碼只有兩個內部 function 是手寫,其餘 Delphi 自動生成。
  5. 小 Bug:點「再來一題」清空文字框時會觸發 submit,導致顯示 Wrong。可用狀態變數全域控制。
  6. 每句必以 ; 結尾,if 內語句亦然,與 Pascal 不同。
  7. 強大 IDE 能判斷大括號層級,避免出錯。
  8. 感覺寫法幾乎與 Windows 原生程式無異,比 IntraWeb 簡單得多。PHP 強,Delphi 更強。
  9. 向導生成的 PHP 碼上傳到 dh 和 surpass 都跑不動。dh 錯誤似為不支援 inc.php 副檔名,無法修改伺服器 PHP 設定,只好自架 Apache 測試。Delphi for PHP 除錯用的就是 Apache,應是設定問題。
  10. 聽說中文支援不好,尚未測試。
  11. 檢查語法正確功能無快捷鍵,不太方便。

繼續慢慢研究中。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved