search
HomeBackend DevelopmentPHP TutorialPHP调用java类的两种方法!_PHP

PHP调用java类的两种方法!_PHP

Jun 01, 2016 pm 12:34 PM
ejbjavaphpapplication servicesmethodmoduletransfer

Java语言功能强大,因此在许多情况下在php中来调用Java的功能将十分有用。在php中调用Java语言有两种方法,一种是使用php中的Java扩展模块,另一种是使用minij2ee应用服务器提供的SJOP协议实现。下面我们来比较一下这两种方法各自的特点。
1.php的Java模块
php发布版中包含一个Java扩展模块,可以用来调用Java对象,例如:
$system=new Java("java.lang.System");
print "Java version=".$system->getProperty("java.version")."
\n";
?>
使用这种方法的优点是比较方便,只要用new Java()来创建一个Java对象,就可以同php类一样来调用Java对象。但是这种方法也有以下明显的缺点:
1.由于php的Java模块根据php的数据类型选择最适合的Java方法,因此无法调用Java过载的函数。
2.php的Java模块将在当前Web Server的进程中载入JVM(Java虚拟机),因此系统开销极大,影响Web Server进程的执行效率。
3.在某些操作系统和Web Server环境中,php的Java模块将使Web Server进程僵死。见http://www.php.net/bugs.php?id=6122。
由于这些原因,php的Java模块一直无法应用到实际的的软件系统中。

2.minij2ee应用服务器SJOP协议实现
在介绍minij2ee应用服务器SJOP协议实现之前,先简单介绍一下minij2ee应用服务器。minij2ee应用服务器是第一款支持php的J2EE应用服务器产品,使php能够用于开发企业级应用系统。SJOP全称是Sample Java ORB Protocol(简单Java对象请求代理协议),是一种简单高效的对象请求代理协议。比如:
$conn=minij2ee_fetch_connection();
print "Java version=".minij2ee_callstatic_javaobj($conn,"java.lang.System","getProperty","java.lang.String","java.version")."
\n";
?>
minij2ee应用服务器实现SJOP协议的主要目的是使php中能够访问EJB企业级组件,因此minij2ee提供了一个EJB-PHP编译器,可以把EJB组件编译成php的类,使php程序中能够方便的调用EJB组件,例如:
require("Cart.php"); file://Cart.php是编译Cart EJB后生成的Cart EJB的php类定义。

$home=new CartHome(); file://创建EJB的Home接口。
$objref=$home->create($cart_name); file://创建Cart EJB。
$cart=new Cart($objref);

$cart->add("some goods");  file://向购物车中添加一个物品。
?>
使用minij2ee应用服务器的php支持,就可以开发出基于php和J2EE技术的,面向对象的,稳定高效的企业级应用系统。关于更详细的PHP-J2EE技术的介绍,请访问http://www.minij2ee.com/document/document_index_6_0.html。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use