Asp-Classic

Asp Classic 包含一次

  • December 10, 2019

ASP Classic 是否具有與 PHP 的“包含一次”功能等效的功能?

我知道這是一個老話題,但我想我會加兩分錢,以防有人感興趣。

我寫了一個函式,它完全符合你的要求:無論呼叫多少次,都將給定的文件完全包含在一個文件中。

class ImportFunction
   private libraries_
   private fso_

   private sub CLASS_INITIALIZE
       set libraries_ = Server.createObject("Scripting.Dictionary")
       set fso_ = Server.createObject("Scripting.FileSystemObject")
   end sub

   public default property get func (path)
       if not libraries_.exists(path) then

           on error resume next
           with fso_.openTextFile(path)
               executeGlobal .readAll
               if Err.number <> 0 then 
                   Response.write "Error importing library "
                   Response.write path & "<br>"
                   Response.write Err.source & ": " & Err.description
               end if
           end with
           on error goto 0

           libraries_.add path, null
       end if
   end property
end class
dim import: set import = new ImportFunction

筆記:

  • 這使用了一個用預設屬性模擬的仿函式。如果這讓您感到困擾,它很容易重構。
  • 字典必須通過所有包含持久化,而持久化 fso 只是避免重建它。如果您不喜歡在導入完成後保留它們的想法,您可以修改類以獲得如下語法:
with new Importer
   .import "foo"
   .import "bar/baz"
end with

引用自:https://stackoverflow.com/questions/813426