Asp Classic包含一次

C. *_*oss 15 include asp-classic

ASP Classic的功能是否相当于PHP的"包含一次"功能?

Tho*_*ith 13

我知道这是一个古老的话题,但我想我会加上我的两分钱以防任何人感兴趣.

我编写了一个能够完全符合您需要的函数:无论调用多少次,都将给定文件包含在内.

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
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 这使用了使用默认属性模拟的虚函数.如果这困扰你,它很容易被重构.
  • 字典必须通过所有包含持久化,而持久化fso只是避免重构它.如果您不喜欢在导入完成后保留它们的想法,您可以修改类以获得如下语法:

    with new Importer
        .import "foo"
        .import "bar/baz"
    end with
    
    Run Code Online (Sandbox Code Playgroud)