教你如何正確產生PHP類檔案_PHP教程

來源:互聯網
上載者:User
我們在通過對PHP語言的學習可以知道,需求:

1、每次為網站鏈入一個新的項目資料,都需要為該項目做改動(拷貝舊項目,改動參數)

2、改動操作不同項目的資料表的不同欄位。

需求分析:

1、可以通過產生不同的程式檔案來實現,也可以通過讀取設定檔來實現。(但第一個現在更適合我的整站架構)

2、需要操作的不同的項目表,在建立項目時記錄到表中,然後在產生介面檔案的時候從配置中讀取到需要操作的表和欄位。

所以呢,我在我的目錄下,專門建立了一個function檔案夾,就放我產生的程式.方便介面自動調用

產生PHP類檔案的測試代碼如下:

 
  1. //搞個項目名稱,就叫foo
  2. $item_name = 'foo';
  3. //產生的程式檔案的位置
  4. $file = './function/'.$item_name.'.php';
  5. //建立程式的函數,擷取到相應項目的相應類函數
  6. $content = createItemProgram($item_name);
  7. //建立檔案
  8. createSoapFunction($file,$content);
  9. //定義建立介面檔案的函數
  10. function createSoapFunction($file,$content)
  11. {
  12. if (!file_exists($file))
  13. { //檔案不存在
  14. $handle = fopen($file, "w");
  15. if($handle)
  16. {
  17. echo '建立檔案成功';
  18. $cont = fwrite($handle, $content);
  19. if($cont === FALSE)
  20. {
  21. echo "不能寫入到檔案 $file";
  22. }
  23. else
  24. {
  25. echo "$file 寫入成功";
  26. }
  27. }
  28. else
  29. {
  30. echo '建立檔案失敗';
  31. }
  32. }
  33. else
  34. { //檔案已經存在
  35. if (is_writable($file))
  36. {
  37. echo '檔案可寫';
  38. $handle = fopen($file, "w");
  39. $cont = fwrite($handle, $content);
  40. if($cont === FALSE)
  41. {
  42. echo "不能寫入到檔案 $file";
  43. }
  44. else
  45. {
  46. echo "$file 寫入成功";
  47. }
  48. }
  49. else
  50. {
  51. echo '檔案不可寫';
  52. }
  53. }
  54. }
  55. //定義建立新檔案的內容
  56. function createItemProgram($pname)
  57. {
  58. $content = '.$pname.'
  59. {
  60. function GetStoppedUsers($dblink, $pidArr)
  61. {
  62. ...
  63. }
  64. function '.$pname.'OnRecord($data,$link,$type)
  65. {
  66. return 'Successful';
  67. }
  68. }
  69. ?>
  70. ';
  71. return $content;
  72. }
  73. ?>

該文只體現了對檔案的寫入操作,產生產生PHP類檔案的函數,根據自己的需求寫商務程序。

上文產生的foo.php檔案內容為:

 
  1. php
  2. class foo
  3. {
  4. function GetStoppedUsers($dblink, $pidArr)
  5. {
  6. ...
  7. }
  8. function fooOnRecord($data,$link,$type)
  9. {
  10. return 'Successful';
  11. }
  12. }
  13. ?>

以上全文就是我們要教給大家的有關產生PHP類檔案的相關代碼。


http://www.bkjia.com/PHPjc/446409.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446409.htmlTechArticle我們在通過對PHP語言的學習可以知道, 需求: 1、每次為網站鏈入一個新的項目資料,都需要為該項目做改動(拷貝舊項目,改動參數) 2、...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.