??ASP is a dynamic web page programming technology introduced by Microsoft at an early stage. However, its ability to combine ADO to provide convenient and fast access to databases, and other technologies such as XML, COM/ActiveX, etc. to realize the multi-layer structure of the server makes it still have a strong hold today. vitality and still has certain development. Although ASP.Net is completely different from ASP in architecture, many of its built-in objects are also extended based on ASP. There are countless articles introducing ASP on the Internet, but few introduce ASP object-oriented and compare it with other languages. This is why I decided to write this article.
??Because it is an early version, ASP only provides a very weak object-oriented interface. As we all know, the implementation language of ASP is divided into VBScript and JavaScript/JScript: In VBScript, there is the Class keyword, which can be used to declare a custom class; JavaScript is weird, it uses a function to "declare" the class, and then in the function Here, properties are defined through this.prototype, and methods are defined through this.func. The discussion here will focus on VBScript. The class declaration of VBScript is as follows:
??Class name
?? statements
??End Class
??You can declare public or private members in statements here, including functions, members and properties. Regarding attributes, I have to praise Microsoft's get and set methods. This concept that appeared in COM has been used until .Net. I personally think that for programmers, it is better to use getProp() and setProp() than Java. It is much more convenient and intuitive to achieve the same effect in one way.
??In comparison, the classes in VBScript are different from the classes in PHP4 (of course they are not comparable to the latest PHP5). The classes in VBScript maintain the incomplete object-oriented "features" of VB. It only implements the most basic Basic constructors/destructors, member functions, variables, properties, and even constructors cannot take parameters. In PHP4, important properties of classes such as inheritance and function overloading are also realized. Only when these are realized can it be called object-oriented and can it provide a basis for realizing polymorphism. But neither of them implements functions such as static members of the class. Although some other modifications can be used to achieve the same effect, from the object-oriented perspective, this is incomplete (because PHP is very flexible, in PHP4, static variables of a class can be indirectly implemented through static variables of member functions; and " ::" - operator that can implement static function access of a class - there is no strict check in PHP4. In other words, all member functions can be accessed as static functions, as long as you do not use member variables in the function No error. VBScript does not implement static at all and can only be implemented using Session or Application). So in normal use, you can use VBScript's custom classes to encapsulate some operations, but don't expect it to serve your object-oriented ideas like C++/Java/.Net.
??VBScript also promotes the good style in VB that the default parameters or variables are references. In this way, although the Script language is not type-sensitive, it can also achieve the same effect of pointers/references in C/C++ and accomplish many things. The most basic one, for example, use it to define the node class ListNode of a list:
<%
Class ListNode
Public Content
Public NextNode
Private Sub Class_Initialize()
Content="Node"
Set NextNode=Nothing
End Sub
End Class
%>
??Haha, it’s that simple, but don’t feel contempt and don’t forget to initialize the variables. It's similar in VB, just add the type when declaring. And when using:
<%
Set nh=new ListNode
Set nh.NextNode=new ListNode
'Other statements...
'Traverse the list
Set n=nh
While Not n is Nothing
Response.Write n.Content+"<br />"
Set n=n.NextNode
Wend
%>
??If no other code is added, the above running result is two "nodes". The same goes for VBScript's custom classes and objects. As long as you master the basic concepts and have a certain understanding of it, it couldn't be easier. Again, using the Set statement to assign a value to an object is equivalent to assignment in Java, which is to obtain a reference. This is much better than the default object assignment in PHP4 which calls the copy constructor to create a new object (even a statement like obj=new Obj; will create two objects! If you want to get a reference, you must put the variable after the equal sign Displayed with &) before it, and it seems that PHP5 does not want to modify this approach of PHP4.
??Session itself in ASP can store objects. It can save basic variables, arrays, automation objects (Automation Objects), etc., but it will encounter problems when storing objects of custom classes. Such as the following code:
<%
If isempty(Session("node")) Then Set Session("node")=New ListNode
Set n=Session("node")
Response.Write n.Content
%>
??Still the ListNode class above, this code is intended to retain only one ListNode object in a user session. Therefore, when the user visits the webpage for the first time, an object of ListNode will be generated and saved in Session ("node"); when the user visits the webpage later, because Session ("node") is not empty, it will not Generate a new object, but retrieve the saved object from Session("node"). Theoretically, 100 should also be output, but here comes the problem, ASP keeps reporting an error:
??Microsoft VBScript runtime error '800a01b6'
??Object doesn't support this property or method: 'n.Content'
??Using n.Type will also cause errors. The same code is translated into PHP and runs successfully. Why?
??After personal analysis, I think it is correct that Session can save objects, but the type conversion mechanism in VBScript is too weak, and there is no explicit forced type conversion for users to use, so Session ("node") cannot be correctly converted to ListNode type. . Because it is a custom class, we can only have the definition statement of the class in every page. In this way, from the perspective of ASP, every time this page is read, the ListNode class is a new class, so it does not recognize the class in the Session. object of this class.
??Conclusion: Try not to use Session or Application to store objects of custom classes in ASP. If you really need it, you can consider using COM to write the class, and then use: Set Session("obj") = Server.CreateObject("YourApp.YourClass") in VBScript to create an object, and then you can achieve the functions envisioned above. .