PHP & Java(2)

原创 2001年01月09日 15:29:00

Example 1: Creating and using your own Classes

Creating your own Java class is pretty easy. Open a new text file called phptest.java. Make sure it is in your java.class.path, as specified in your php.ini file. In that file, enter the following:

public class phptest{ 
	/**
	* A sample of a class that can work with PHP
	* NB: The whole class must be public to work,  
	* and of course the methods you wish to call
	* directly.
	*
	* Also note that from PHP the main method 
	* will not be called    
	*/
	
	public String foo; 
	
	/**
	* Takes a string and returns the result 
	* or a msg saying your string was empty
	*/
	public String test(String str) {      
		if(str.equals("")) { 
          		str = "Your string was empty. ";      
		}      
		return str;    
	}
	
	/** 
	* whatisfoo() simply returns the value of the variable foo.
	*/     
	public String whatisfoo() {      
		return "foo is " + foo;    
	} 
	
	
	/** 
	* This is called if phptest is run from the command line with 
	* something like 
	*   java phptest
	* or
	*   java phptest hello there
	*/
	public static void main(String args[]) {
		phptest p = new phptest();
        
		if(args.length == 0) {
			String arg = "";
			System.out.println(p.test(arg));
		}else{
			for (int i=0; i < args.length; i++) { 
				String arg = args[i];
				System.out.println(p.test(arg));    
			}
		}
	}
}

Once you have created the file, you want to compile it with javac phptest.java at the command line. This will depend on having the java/bin directory in your PATH environment variable. Once compiled, you can test the class on the command line. Try java phptest, or java phptest hello world. This class is made accessible on the command line, via the main() method. This isn't required for our Java + PHP explorations, but is a nice way to see the class at work.

To test this new class with PHP, create a new php file on your web server called phptest.php. It should contain the following:

<?php

  $myj 
= new Java("phptest");
  echo 
"Test Results are <b>" $myj->test("Hello World") . "</b>";
  
  
$myj->foo "A String Value";
  echo 
"You have set foo to <b>"     $myj->foo "</b><br>/n";
  echo 
"My java method reports: <b>" $myj->whatisfoo() . "</b><br>/n";
  
?>

If you get a Warning: java.lang.ClassNotFoundException error, it simply means your phptest.class file is not in your java.class.path specified in your php.ini file.

One thing to remember is that Java is strongly typed, and PHP isn't. This could cause problems when Java expects a String, but receives an Integer (or visa-versa). Try replacing the line: $myj->foo = "A String Value"; with $myj->foo = 12345678; and see what happens.

You should cast your variables to the correct type before passing them to Java. eg: $myj->foo = (string) 12345678; or $myj->foo = "12345678";

The phptest.java example is a very simple example of how you can create you own Java class and have PHP access to it.

关于 php 里面的break 2

$arr = array('one', 'two', 'three', 'four', 'stop', 'five'); while (list(, $val) = each($arr)) { ...
  • lijingshan34
  • lijingshan34
  • 2017年04月17日 20:12
  • 357

PHP保留2位小数

JS保留两位小数例子 四舍五入使用函数toFixed()document.write("JS保留两位小数例子");var a=2.1512131231231321;document.write("原来...
  • zl8762385
  • zl8762385
  • 2010年11月01日 16:29
  • 1355

PHP数值的保留2位小数和四舍五入

$price = 123; 1、return round($price,2); 2、return number_format($price, 2, '.', '');第一种方法是四舍五入并保留2位小数...
  • akazhuk
  • akazhuk
  • 2016年02月20日 14:14
  • 603

PHP 计算下个月当天日期,解决跨年和2月份的各种问题

public function fq_time($time){ $rq; $arr=getdate($time);//判断月份 $year=$arr['year']; $month=$arr[...
  • fgdfgasd
  • fgdfgasd
  • 2015年11月16日 02:39
  • 1565

PHP中一个 & 和两个 && 的区别简析

两个 && 是逻辑 “与”。 一个 & 是按位“与”。几个例子:$arr = []; if (($arr[1] = 1) & ($arr[1] == 1) & ($arr[1] = 3)) { ...
  • qq_16885135
  • qq_16885135
  • 2017年07月26日 14:17
  • 362

php zip解压 unpack错误解决方案

php zip.class.php 解压报 unpack错误解决方案 Type v: not enough input, need 2, have 0 zip unpack 解决办法正常的在wi...
  • kof820
  • kof820
  • 2016年07月13日 13:11
  • 829

HTTP/2 协议用于 iOS 推送提醒服务 (APNS)

苹果最近更新了他们的推送提醒服务协议,APNS。这个新版本的协议基于HTTP/2和JSON,相比于旧的二进制协议,新的协议有了巨大改进。新的APNS协议基于HTTP/2: 新的特性和功能: 基于JS...
  • Rodgexue
  • Rodgexue
  • 2017年02月16日 14:13
  • 696

PHP函数十进制、二进制、八进制和十六进制转换

PHP函数篇详解十进制、二进制、八进制和十六进制互相转换函数说明,主要掌握各进制转换的方法,以应用于实际开发。 一,十进制(decimal system)转换函数说明 1,十进制转二进制 de...
  • lyd518
  • lyd518
  • 2013年08月26日 15:54
  • 4679

PHP连接两个数据库,连接另一个数据库

现在的项目里面突然需要另一个ip地址的数据库中的表里面的数据 在config文件里写 'OTHER_DB_CONFIG'=>'mysql://root:psw@192.168.1:3306/表#u...
  • u012521571
  • u012521571
  • 2017年09月04日 15:50
  • 248

PHP实现保留两位小数的三种方法

$num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2); //10.46 //第二种:利用spri...
  • AnPHPer
  • AnPHPer
  • 2017年10月23日 09:25
  • 876
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PHP & Java(2)
举报原因:
原因补充:

(最多只允许输入30个字)