4、实例测试
以下两个例子摘自PHP MANUAL。
1) Java Example
<?php
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
// demonstrate property access
print 'Java version='.$system->getProperty('java.version').' ';
print 'Java vendor=' .$system->getProperty('java.vendor').' ';
print 'OS='.$system->getProperty('os.name').' '.
$system->getProperty('os.version').' on '.
$system->getProperty('os.arch').' ';
// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
print $formatter->format(new Java('java.util.Date'));
?>
在CGI或CLI中运行上述代码,结果如下:
Java version=1.4.2_04
Java vendor=Sun Microsystems Inc.
OS=Windows 2000 5.0 on x86
星期四, 七月 15, 2004 at 9:00:23 下午 中国标准时间
2) AWT Example
<?php
// This example is only intented to be run as a CGI.
$frame = new Java('java.awt.Frame', 'PHP');
$button = new Java('java.awt.Button', 'Hello Java World!');
$frame->add('North', $button);
$frame->validate();
$frame->pack();
$frame->visible = True;
$thread = new Java('java.lang.Thread');
$thread->sleep(10000);
$frame->dispose();
?>
运行结果如下图所示,是不是已经勾起你的兴趣了呢?