Migrate from PHP to ASP.NET

zhaozj2021-02-16  114

Migrate from PHP to ASP.NET

Microsoft Corporation

September 2003

Suitable for: Microsoft? ASP.NETMICROSOFT VISUAL BASIC? .NET

Summary: Learn how to migrate from PHP to ASP.NET to learn about the characteristics, functions, and architectures of these two systems. (This article contains some links to English sites.)

table of Contents

Introduction Architecture comparison function compared grammatical and common task compared data cache and page cache summary recommendations subsequent operational resources

Introduction

This article discusses the migration from PHP (PHP: Hypertext Preprocessor 4) to ASP.NET, and compares the basic syntax and basic functions and architectures of PHP and Microsoft? Visual Basic? .NET.

Although complex web applications (such as e-commerce sites, intranet, and enterprise portals) can be constructed, but there are several important differences between the two. Unlike PHP, ASP.NET is not a language or analyzer, but a group of Microsoft .NET Framework to build web applications and XML Web Service. Like php, the Microsoft ASP.NET page performs and generates tags (eg HTML, WML or XML) on the server, and the tag will be sent to the desktop application or mobile application. However, the ASP.NET provides a powerful, object-oriented and event-driven programming model for the development web page, while still retaining the simplicity of PHP developers.

The ASP.NET application is based on power-oriented programming (OOP) mode, not based on scripting mode. This allows more rigorous OOP functions such as inheritance, packaging and reflection. Although most basic simple operations can easily convert from PHP to ASP.NET, but to convert more complex applications is not as simple, this requires careful planning and consideration, and uses more in line with OOP.

This article assumes that the reader has PHP experience and has a basic understanding of programming and software development. This paper first briefly compares the difference in the basic architecture of the two from the code, and briefly introduces the OOP development model, then compares the functions of both, and finally uses PHP and ASP.NET development web applications. Syntax and common The task is compared.

Note: If you want to skip more information about migration, just try ASP.NET, you can jump directly

It is recommended to follow up.

Architecture comparison

After reading this article, you will learn about PHP and ASP.NET through comparative grammar and language, and they have similar functions and syntax. However, from the more basic architecture level, PHP is very different from ASP.NET. PHP is based on a platform-standing processor / engine, which is used to analyze PHP scripts, providing database connections, complying with Internet protocols, and providing many other tasks that are common to provide most web application platforms.

ASP.NET is a framework built based on a series of technologies such as CLRs, providing a large number of class libraries organized in a very good way, which meets the needs of most features to be used in web applications. You can also easily create a variety of components to extend the framework.

Although PHP also provides a similar function (such as a PEAR library), PHP and ASP.NET are not identical because the ASP.NET framework is based entirely OOP mode and OOP concept, while PHP is not. This difference is obvious in terms of PHP and ASP.NET's access method of category and objects. Object-oriented programming in PHP and ASP.NET

Although PHP and ASP.NET provide OOP mode for application development, they have different support for various OOP concepts such as packages and polymorphisms. For example, PHP only supports partial packaging (eg, supporting methods and fields in classes) and some polymorphisms (no overload and abstraction). In addition, PHP does not support these concepts and their access due to the concept and overloaded functions such as private (PUBLIC) or Protected (Protected) functions in the PHP class. OOP pureists may say that ASP.NET and various languages ​​have not fully supported all the concepts in OOP mode, which is true for most languages ​​(such as C and Java) using OOP.

This has both a positive side and has a negative side. Negative side is: For some web developers, learning ASP.NET is difficult than learning PHP, because PHP is provided by developers' script mode in which the developer is creating a Web site. However, developers who have used OOP language and / or V will find ASP.NET is relatively intimate and easy to learn.

ASP.NET supports the positive side of the OOP concept is that most parts of the ASP.NET application will be well designed and can be clearly separated from content, logic, and data, thus long-term perspective from the application lifecycle. Look, this is often easier for it to support it. In addition, since the ASP.NET itself supports corporate technology, such as message queues, transactions (see the .NET Framework System.EnterpriseServices) SNMP and Web services, it can easily develop powerful applications with high scalability.

To roughly understand the main concepts of object-oriented programming (from Visual Basic's angle), see Object-Oriented Programming In Visual Basic.

Compile

PHP

When the PHP page requests the PHP page, the HTML and Inline PHP scripts will be compiled into Zend OpCode. OpCode is a low-level binary directive, which is the code that is actually used when the PHP page is provided. After compiling, the Zend engine will run opcode (Similar to the Java runtime engine) and then generate HTML and supplied to the client.

Many commercial products can be used to speed up the execution speed of the PHP page by optimizing these OPCODE. Other methods for improving PHP scripts also include cache OpCode and generated HTML.

ASP.NET

This request will be transferred to the ASP.NET when requesting the .aspx page (or any other extension supported by ASP.NET). If it is the first time requesting this page, ASP.NET compiles it to MSIL (Microsoft Intermediate Language). Then, the CLR (public language runtime) converts this MSIL code into machine code. Finally, use this compiled code to run the request. Subsequent requests will use the same machine code (assuming that the page is not modified). It should be noted that because the binary code generated by the CLR has been optimally optimized, there is no need to use other products to help improve performance.

Also note that all content in ASP.NET is compiled as machine code before running. Even HTML text is also converted to a string value control and inserted into the control tree in the appropriate order.

Function comparison

Table 1 compares some of the main functions in PHP and ASP.NET.

Table 1: Comparison of PHP and ASP.NET

Function phPasp.net encoding languages ​​have an old ASP style tag, a C, C style scripting language. Support some OOP concepts. Supports more than 25 languages, but the most commonly used languages ​​are Visual Basic .NET and C #. Most developers choose a language, but you can use components written in any other supported language. Compileable application logic can be compiled and can be run as an executable program. Support in dynamic compilation and pre-compilation mode. All page output cache itself is not supported. Support, different versions of the page based on one or more URL parameters, browser types, custom functions, or any combination. Some page output cache itself is not supported. Provide built-in support by using user controls. Data and other objects can be cached using cache API and complex expiration rules. Database Access provides drivers for most databases on the market and the open source database. It also supports OLE-DB and ODBC, which also contains Microsoft SQL Server? And Oracle drivers. The database output dataset is returned as a PHP variable and outputs it like any other variable. Bind to the server-side control in template to simplify development; if desired, you can manually loop. External components can call various packages, and developers can directly modify the Zend Engine (because it is open source). Responsible for Zend programming personnel can also make the .NET object to be called from the PHP. Fully support your own C library and COM objects and an assembly written in any language (including managed C [Managed C ]). Built-in support is not provided for CORBA objects or Java classes. XML / XSLT supports a broad and easy-to-use support for XML DOM, XSLT, verification, and XML documents in additional packages and libraries. XML Web Service is currently developing a PHP package to support a web service based on Apache Axis engine and other engines. The current version provides extensive, flexible, compliant with standard support, making it easy to publish and use Web services. Session status Based on cookie session management Based on cookie or without cookie (using a single server memory storage, central status server, or database backend). In addition, the scalable architecture also allows replacement of built-in options using a custom session status module. To enable a session without a cookie, simply change the configuration. Built-in functional PHP's built-in features cover many common tasks that may need to be implemented based on web-based applications. With some additional processing, it can also access the Java class library. ASP.NET can directly access the entire .NET Framework class library, which contain a lot of functions. Regular expressions support POSIX and Perl compatible regular expressions syntax. Supports Perl-5 compatible regular expressions, as well as some other features, such as from right to left, precompiled expressions, naming groups, and complete Unicode support. In addition, the user is allowed to specify a function called during the regular expression replacement operation. Debug PHP does not provide a wide range of debugging capabilities, but you can use a variety of third-party products and Zend to enhance the debugging and testing function. Contains extensive tracking and environmental information, which can be included in the page or displayed in separate pages. Use Microsoft Visual Studio? Net In addition to the client script and SQL Server stored procedure, you can easily debug it when you execute the page. Error handling does not support error capture, but various error handling functions and logs are provided. Support structured exception handling (which added a "Finally" code block, regardless of whether an error occurs, is executed), supporting the trigger custom exception, but also supports a custom error page for different types of unrecoverable errors . Image processing has no built-in support, but third-party components can be used.

Contains a large number of tools for creating and processing images. (See the .NET Framework system.drawing class.) Code Reuse user-defined functions, create classes, and contain files. User control, server control, custom class, and containing files. Thread PHP has a good thread model. fully support. Not only can you request multiple pages simultaneously in a separate thread, but also each page (if needed) can also generate your own thread to perform synchronous or asynchronous operations. The data cache has a limited data cache (limited to the data itself). With a large number of cache APIs, almost any type of data (including database query results), and can expire based on time, usage, file, or other cache item. The user-defined function is also allowed to call the user-defined function when it is deleted from the cache. Internationalization fully supports Unicode. Support Unicode strings and various character encodings. Dates, numbers, and monetary functions can distinguish between cultural environments, and change the output based on the current cultural environment (without calling other functions). Support for using resource files to dynamically localize the application. SMTP, HTTP, FTP, POP3 itself supports a variety of Internet protocols. SMTP supports the IIS SMTP service. HTTP can be supported well. .NET does not support FTP and POP3, but there are some free and sales of third-party components that can be used to achieve this. The integrated development environment can use a large number of development tools that provide a wide range of functions, and the price is from zero to a few hundred dollars. Microsoft offers a free ASP.NET development tool called ASP.NET Web Matrix. The most common tool is Visual Studio.net, which provides full support for all .NET languages, database tools for creating SQL and test databases, web design tools, integration, advanced debugging features, and many other features. . To get a full function list, visit the MSDN? Visual Studio Developer Center. Other tools (including Borland C # Builder and Macromedia Dreamweaver MX) also support ASP.NET. Web Server supports almost all web servers. Support IIS and Covalent's commercial version Apache 2.0. Operating system supports almost all commonly used operating systems, including Microsoft Windows?, Mac, OS X, Amiga, Solaris, Free BSD, Linux, and AIX. Currently supports Windows 2000, Windows XP, and Windows Server 2003. Comparison of grammar and common tasks

The following parts have compared the grammar of PHP and .NET, and introduce how to complete some very common programming tasks.

Comment

PHP allows you to insert a comment in the code using C, C , and UNIX shell style syntax, and any content in the comment indicator will not be executed.

Generally speaking, you want to note the Visual Basic .NET code in ASP.NET, you need to use <% - to open the comment block, and use-%> Close the comment block.

Table 2 shows comments in both environments.

Table 2: Server Note

Phpasp.net/* This is a text block * / <% - this is a comment -%>

variable

Although PHP and Visual Basic .NET have similar language construction, the syntax is very different. Since Visual Basic .NET is built based on an OOP model, the variable declares is more rigorous than the declaration of variables in PHP (just add a $ symbol before the variable name is declared) in PHP.

When declaring variables in Visual Basic .NET, you want to specify its variable name and feature. The declaration statement of the variable is a DIM keyword. Its position and content determine the characteristics of the variable. Variables have levels (eg, local variables and module variables), data type, survival, and accessibility. Although this approach appears to be complicated than specifying variables in PHP, it is actually convenient to developers. ASP.NET is committed to helping developers create reliable applications, and the specified data type can make tasks such as variable cleaning, debugging, exception, and error handling, and code maintenance. Simple.

Table 3 shows an example of declaring variables in two environments.

Table 3: Variable declaration

PHPVisual Basic .NET $ Head_count $ foo $ x $ objdim Head_count as integer Dim Foo As String Dim x as date limited

Declare data type

The AS clause in the statement statement is used to define the data type or object type of the declared variable. You can specify any type of variable:

Basic data types, such as Boolean, Long, or Decimal composite data type, such as an array or object type or class in Visual Basic or other applications, such as label or TextBox

You can declare multiple of the same type of variables in a statement without having to repeat the data type. The following statement declares Numstudents, Numgta, and Numprofessors as an Integer type:

DIM Numstudents, Numgta, Numprofessors As Integer 'The three variables are Integer types.

For more information on data types, see Data Types. For more information on object-oriented programming, see Object-Oriented Programming IN Visual Basic.

Declaration of survival

The survival period of the variable refers to the time period for variables available. The local variables declared using the DIM statement exists only during their execution. When the process terminates, all local variables of the process will disappear, and the values ​​of these variables will also be lost.

This concept is useful because it makes developers who do not have to consider many of the problems in large applications when creating applications, such as valid memory management. By selecting the correct survival for variables, you can make the .NET to perform cleanup operations without using the variables used.

For more information on survival, see Lifetime.

Declare

The local variable is a variable declared in the process (similar to the function). Non-local variables are variables declared in the process but declared in the class or structure.

In the class or structure, the category of the non-local variable depends on whether the variable is shared. Variables with Shared keyword declared are shared variables, which only have a copy and share it in all instances of the class or structure. If you do not use the Shared keyword, the variable is an instance variable, and each instance of the class or structure will create a separate variable copy. The given copy of the instance variable can only be used to create an instance of the copy.

The range of variables refers to all code sets that can be referenced without limiting their name. The range of variables depends on the position of the declared variable. Codes located in a given area can use variables defined in this area without having to define their names. The following rules will be followed when the declaration range:

A shared variable or example variable ranges to declare the structure or class of the variable. The range of local variables is to declare the process of this variable.

However, if you declare a local variable in the block, its range will be limited to this block. The local variable is active in the control block defined. The control block can be a process, if statement, and a loop statement.

For more information on scope, see Scope.

Disclaimer accessibility

.NET supports the accessibility of the variable, so that the developer can control which code can access a specific variable. For example, if you want to set some constants for a formula, be sure that these constants do not be changed by other code other than they are class, you can declare this variable as follows: Private MyConstant AS Integer

Variable accessibility is determined by one or more keywords DIM, PUBLIC, PROTECTED, FRIEND, PUBLIC, PRIEND, or Private, Protected, or Private of the declaration statement. Public and private is usually only used during development.

You can declare modules, structures, classes, or instance variables using any of the above keywords. Only DIM keywords are allowed during the process, and accessibility is always private.

Output

A typical way of output data in PHP is to use an ECHO () language structure. ASP.NET is most similar to this is the response.write () method (or <% =%> constructor, which is just a shortest form of Response.write ()). Table 4 shows the basic syntax of the write value to the page.

Table 4: Basic output

PHPVISUAL BASIC .NET <% Dim Hello As String = "Hi How Are You" & Vbcrf Response.write (Hello)%>

However, these methods for transmitting output to the browser are mainly to maintain backward compatibility with traditional ASPs. ASP.NET provides new control-based and event-oriented models, you only need to set up data to your browser on the server control. By this technique can clearly separate layouts and code to simplify maintenance work, and the code required in complex circumstances is much less than PHP.