php&java(二)
2023-06-13 09:13:44 时间
例子1:创建和使用你自己的JAVA类
创建你自己的JAVA类非常容易。新建一个phptest.java文件,将它放置在你的java.class.path目录下,文件内容如下:
publicclassphptest{
/**
*AsampleofaclassthatcanworkwithPHP
*NB:Thewholeclassmustbepublictowork,
*andofcoursethemethodsyouwishtocall
*directly.
*
*AlsonotethatfromPHPthemainmethod
*willnotbecalled
*/
publicStringfoo;
/**
*Takesastringandreturnstheresult
*oramsgsayingyourstringwasempty
*/
publicStringtest(Stringstr){
if(str.equals("")){
str="Yourstringwasempty.";
}
returnstr;
}
/**
*whatisfoo()simplyreturnsthevalueofthevariablefoo.
*/
publicStringwhatisfoo(){
return"foois"+foo;
}
/**
*Thisiscalledifphptestisrunfromthecommandlinewith
*somethinglike
* javaphptest
*or
* javaphptesthellothere
*/
publicstaticvoidmain(Stringargs[]){
phptestp=newphptest();
if(args.length==0){
Stringarg="";
System.out.println(p.test(arg));
}else{
for(inti=0;i<args.length;i++){
Stringarg=args[i];
System.out.println(p.test(arg));
}
}
}
}
创建这个文件后,我们要编译好这个文件,在DOS命令行使用javacphptest.java这个命令。
为了使用PHP测试这个JAVA类,我们创建一个phptest.php文件,内容如下:
<?php
$myj=newJava("phptest");
echo"TestResultsare<b>".$myj->test("HelloWorld")."</b>";
$myj->foo="AStringValue";
echo"Youhavesetfooto<b>" .$myj->foo."</b><br>n";
echo"Myjavamethodreports:<b>".$myj->whatisfoo()."</b><br>n";
?>
如果你得到这样的警告信息:java.lang.ClassNotFoundExceptionerror,这就意味着你的phptest.class文件不在你的java.class.path目录下。
注意的是JAVA是一种强制类型语言,而PHP不是,这样我们在将它们融合时,容易导致错误,于是我们在向JAVA传递变量时,要正确指定好变量的类型。如:$myj->foo=(string)12345678;or$myj->foo="12345678";
这只是一个很小的例子,你可以创建你自己的JAVA类,并使用PHP很好的调用它!
创建你自己的JAVA类非常容易。新建一个phptest.java文件,将它放置在你的java.class.path目录下,文件内容如下:
publicclassphptest{
/**
*AsampleofaclassthatcanworkwithPHP
*NB:Thewholeclassmustbepublictowork,
*andofcoursethemethodsyouwishtocall
*directly.
*
*AlsonotethatfromPHPthemainmethod
*willnotbecalled
*/
publicStringfoo;
/**
*Takesastringandreturnstheresult
*oramsgsayingyourstringwasempty
*/
publicStringtest(Stringstr){
if(str.equals("")){
str="Yourstringwasempty.";
}
returnstr;
}
/**
*whatisfoo()simplyreturnsthevalueofthevariablefoo.
*/
publicStringwhatisfoo(){
return"foois"+foo;
}
/**
*Thisiscalledifphptestisrunfromthecommandlinewith
*somethinglike
* javaphptest
*or
* javaphptesthellothere
*/
publicstaticvoidmain(Stringargs[]){
phptestp=newphptest();
if(args.length==0){
Stringarg="";
System.out.println(p.test(arg));
}else{
for(inti=0;i<args.length;i++){
Stringarg=args[i];
System.out.println(p.test(arg));
}
}
}
}
创建这个文件后,我们要编译好这个文件,在DOS命令行使用javacphptest.java这个命令。
为了使用PHP测试这个JAVA类,我们创建一个phptest.php文件,内容如下:
<?php
$myj=newJava("phptest");
echo"TestResultsare<b>".$myj->test("HelloWorld")."</b>";
$myj->foo="AStringValue";
echo"Youhavesetfooto<b>" .$myj->foo."</b><br>n";
echo"Myjavamethodreports:<b>".$myj->whatisfoo()."</b><br>n";
?>
如果你得到这样的警告信息:java.lang.ClassNotFoundExceptionerror,这就意味着你的phptest.class文件不在你的java.class.path目录下。
注意的是JAVA是一种强制类型语言,而PHP不是,这样我们在将它们融合时,容易导致错误,于是我们在向JAVA传递变量时,要正确指定好变量的类型。如:$myj->foo=(string)12345678;or$myj->foo="12345678";
这只是一个很小的例子,你可以创建你自己的JAVA类,并使用PHP很好的调用它!
相关文章
- stringutils中的isempty和isblank_java stringbuilder和stringbuffer
- long转string java_java中long如何转成String????????
- Java详解:淘宝秒杀脚本java
- java图书馆新地址_值得你关注的16个顶级 Java 开源项目!小白必看
- java axis_Java 使用Axis实现WebService实例
- 【JAVA面试必会】JMM高并发详解(java内存模型、JMM三大特征、volatile关键字 )「建议收藏」
- Java项目分享-10个超好看后台管理面板,再也不愁UI了
- Java生成随机数组_java生成唯一数字
- java+sm4+加密算法_SM4加密算法实现Java和C#相互加密解密
- java的NIO编程
- JAVA 新提案:努力简化Hello World,让初学者更好地接受 Java !
- Docker容器上Spring Cloud微服务相互调用时报错:java.net.UnknownHostException:主机名详解编程语言
- 解决Java程序连接MySQL的准确方案:Java连接MySQL的Jar包(java连接mysql的jar包)
- Java实现文本创建、删除、编辑内容详解编程语言
- php转java 系列2 Spring boo 链接数据库jdbc详解编程语言
- 深入浅出Java配置MySQL数据库(java配置mysql)
- 时间解决Java使用Redis设置过期时间的问题(redisjava过期)
- Java调用Linux库实现跨系统功能(java调用linux库)
- 篇MySQL与Java实现长篇应用程序(mysqljava长)
- Java模拟Oracle实现稳定数据库性能(java模仿oracle)
- MySQL和Java结合的必备工具MySQL下载Java(mysql下载java)
- 本使用Oracle Java 进行升级新版本带来新体验(oracle java版)