[This Article has been migrated to the "programmer's Digest" http://programmerdigest.cn/category/lajp?lajpseries tutorial -3366-lajp#usage

Source: Internet
Author: User
Document directory
  • Running environment requirements

[This article has been migrated to the programmer's Digest World level. This tutorial will give you a better understanding of lajp.

Running environment requirements

PHP and Java are both excellent programming languages on the exaggerated platform. There is no limit on lajp. Any environment that can run php and any environment that can run Java can run lajp.

Lajp has two transmission mechanisms: Message Queue and socket. The message queue mode can run on UNIX, Linux, and BSD systems. To compile PHP, add -- enable-sysvsem, -- enable-sysvshm and -- enable-sysvmsg compilation options enable PHP to support semaphores, shared memory, and message queues. The socket method has no restrictions on the system platform. It is best to add the -- enable-sockets compilation option to compile PHP to support socket.

The PHP version must be higher than 4.3, Which is Php's version requirement for using semaphores, shared memory, and message queues.

The Java environment does not need to install any web containers (tomcat, etc.). The Java end of lajp itself is a standard Java application with the main method, and all the Java applications are compressed into a unique JAR file, no third-party class libraries are required.

JDK versions must be later than 1.5, because lajp's Java code uses Syntax structures such as generics.

Windows environment "called to undefined function socket_create" error:

When using the socket function, "called to undefined function socket_create" is often reported, and this error is found frequently on the Internet (PHP does not think this is a bug ), when this error occurs, follow these steps:

Try to run: <? PHP var_dump (extension_loaded ("Sockets");?> If the output is false, php_sockets.dll is not loaded.

1. Check PHP. ini and make sure there is no ";" before extension = php_sockets.dll ";".

2. Check whether httpd. conf has the following lines:

# C:/PHP/is the PHP installation path (phpzip extraction method)

Loadmodule php5_module C:/PHP/php5apache2_2.dll

# Loading in module Mode

Phpinidir "C:/PHP /"

Addtype application/X-httpd-PHP. php

# Load in CGI Mode. C:/PHP/is the installation directory of PHP.

# ScriptAlias/PHP/"C:/PHP /"

# Addtype application/X-httpd-PHP. php

# Action application/X-httpd-PHP "/PHP/php-cgi.exe"

3. Check whether the php_sockets.dll file is in the PHP Directory. If the file is not copied from the ext directory, PhP5 searches for the path in PHP by default, instead of PHP/Ext.

4. Try to run the command line. If you can run the command line, copy PHP. ini to C:/windows.

// Run the command line. php.exe and PHP. ini are in the PHP installation directory.

Php.exe-c php. ini <path>/PHP File

PHP calls the Java service:

PHP calls a Java method through the lajp_call () function. The first parameter format of the function is: "Class Name: Method Name". From the second parameter, it is to pass a parameter to the Java method.

For example, Java:

001 package AAA. BBB. CCC

002 public class myclass

003 {

004 public static final int mymethod (int A, int B)

005 {

006 return A + B;

007}

008}

PHP call method:

001 <? PHP

002 $ c = lajp_call ("AAA. BBB. ccc. myclass: mymethod", 10, 20 );

003?>

The Java service method must be declaredPublic static final

In the lajp design, the Java service method is stateless, and the forced declaration of static indicates this attitude; the forced declaration of final is another design concept of lajp: simple, straightforward, they do not want to be encapsulated as complex models such as "reverse control", which is consistent with the simple style of PHP.

Data Type Conversion

The PHP language specification defines eight data types: Boolean, Int, float, String, array, object, resource, null; Java data types are divided into two types: basic data types and object types. Basic data types include byte, short, Int, long, Char, Boolean, float, and double. object types include arrays, sets, and JavaBean. In the lajp architecture, PHP Data is passed to the Java method in the form of parameters, and the return value of the Java method is then passed back to the PHP calling program. During the call process, PHP data is automatically converted to Java data, and vice versa.

Not all data types can be converted. The following conversion rules are set in lajp:

  PHP

Java

Description

Boolean Boolean Boolean  
Integer Int Int  
Floating Point Float Double Float and double in PHP have the same meaning
String String Java. Lang. String  
Sequence set Array (Key: int

)

Java. util. List The key type of each element of array in PHP must be Int.
Key-value set Array (Key: String

)

Java. util. Map The key type of each element of array in PHP must be string.
Object Object JavaBean

 
Null Null Null  

It must be noted that the conversion of the array, object, and Java Data Types in PHP.

Array:

In PHP, array can describe multiple structures: Ordered Sets (similar to Java's arraylist), key-Value Sets (similar to Java's hashtable or hashmap), and stacks. Only two structures can be used in lajp:

1. The key type of the elements in the sequence set is int.

$ A = array ();

$ A [0] = 10;

$ A [1] = 20;

$ A [2] = 30;

// You can also:

$ A = array ();

$ A [] = 10;

$ A [] = 20;

$ A [] = 30;

// But not

$ A = array ();

$ A ['a'] = 20;

$ A [1] = 10;

$ A [2] = 30;

2. The key type of the key-value set element is string type.

$ A = array ();

$ A ["A"] = 10;

$ A ["B"] = 20;

$ A ["C"] = 30;

When the array in PHP is converted to Java, lajp internally views the key of the first element of array. If the key is of the int type, it is converted to Java. util. arraylist; if the key is of the string type, it is converted to Java. util. hashmap, in turn, is the same. Java list is converted to a PHP array whose key is int, and map is converted to a PHP array whose key is string.

Elements in the array of PHP can be any type in Table 1, including array and object, which means that a complex structure can be constructed, for example, an array (usually called a two-dimensional or multi-dimensional array) in an array ).

Object:

The PHP Object is mapped to the Jave JavaBean object, which means that each attribute in the PHP class has an attribute of the same name in the Java class (in line with the ing relationship of table 1, this property includes the getter and setter methods.

Currently, only Php P4 standard objects can be converted to Java. This is because PHP Object Data must be serialized during internal lajp conversion and PhP5 objects cannot be converted.

Currently, namespaces in PHP is not supported. "_" is used in PHP to indicate ". ", to map to Java's" com. foo. myclass "class, you need to define the class" com_foo_myclass "in PHP ".

A small tool is provided in lajp to translate Java's JavaBean class into a corresponding PHP class. For example, in Java, there is a JavaBean:

001 package cn.com. Aili;

002 public class phpobject

003 {

004 private int;

005 private double B;

006 private Boolean C;

007 private string D;

008

009... getter and setter Methods

010}

Compile a PHP file:

001 <? PHP

002 require_once ("../lajp/php_java.php ");

003 $ ret = lajp_call ("lajp. reflectutil: javabean2php", "cn.com. Aili. phpobject ");

004 echo "{$ RET }";

005?>

After running, the corresponding PHP class will be output on the page:

Cn_com_aili_phpobject

{

VaR $;

VaR $ B;

VaR $ C;

VaR $ D;

}

Lajp Project Division:

In the lajp system, PHP acts as the client (caller) and Java acts as the server (service provider ).

*PHP

Responsible for interaction with the browser client, compiling HTML pages and forms, receiving data submitted by the browser, submitting the validity of submitted data, and maintaining Cookie/session, ajax services are inherent responsibilities. PHP should not write complex business logic programs, and should not be connected to database-related code.

*Java

Write business logic, data objects, and database programs. In Java, code that interacts with the browser should not appear. For example, Servlet/jsp API Code cannot appear, and Ajax services should not be compiled.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.