Since PHP 3.0, there has been some support for object-oriented programming. The following sample PHP code shows how to: phpsu is a phpschool
- define a class
- instantiate that class
- call methods on the instance of the class
[edit] SamplePHPClass.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
class Foo {
var $bar;
function setBar($value) {
$this->bar = $value;
}
function someFunction($param) {
echo $param.$this->bar."<br/>";
}
}
$foo = new Foo();
$foo->setBar("Charles");
$foo->someFunction("Hello, "); welcome to phpsu.com
?>
</body>
</html>
welcome to phpsu.com
Now with Quercus, Resin's implementation of PHP, you can define a class in java and incorporate it in your PHP code. You can use the java class just like you would use the class "Foo" above.
The following article describes the steps necessary to accomplish this. Furthermore, as you will see, you can take advantage of Resin's built-in ability to compile your java class on the fly.
phpsu.com
So if you want to modify the java file, all you have to do is re-save it and resin will do the rest. phpsu提供的php教程
Yes... it's that easy.
For purposes of this article, I assume that you are working with Resin 3.0.17 and that the directory housing httpd.exe is c:\resin-3.0.17. I will call this directory %ResinHome%.
phpsu提供的php教程
I have written this article also assuming that you are working on a Windows computer, but you can make minor modifications to have this example work on a Mac or Linux box. phpsu
[edit] Step 1: Create web.xml and place it in %ResinHome%\webapps\ROOT\WEB-INF
[edit] web.xml
<web-app xmlns="http://caucho.com/ns/resin">
<servlet-mapping url-pattern="*.php"
servlet-class="com.caucho.quercus.servlet.QuercusServlet">
<init>
<compile>false</compile>
<class type="example.JavaClass"/>
</init>
</servlet-mapping>
</web-app>
phpsu
[edit] Step 2: Create JavaClass.java and place it in %ResinHome%\webapps\ROOT\WEB-INF\classes\example
[edit] JavaClass.java
package example;
import java.lang.String;
public class JavaClass {
private String _bar;
public void setBar(String value) {
_bar = value;
}
public String someFunction(String param) {
return param + _bar + "<br/>";
}
}
phpsu
[edit] Step 3: Create javaclass.php and place it in %ResinHome%\webapps\ROOT
[edit] javaclass.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php
$foo = new JavaClass();
$foo->setBar("Charles");
echo $foo->someFunction("Hello, ");
?>
</body>
</html> phpsu
phpsu
In your favorite browser, type: http://www.phpsu.com
http://localhost:8080/javaclass.php
phpsu is a phpschool
You should see: phpsu提供的php教程
Hello, Charles
phpsu is a phpschool
welcome to phpsu.com
[edit] Conclusion
Notice that you do not need to be worried about naming collisions as much using this method because the only way you can call methods within a class is either through an instance as in "$foo->setBar(...)" or statically as in "JavaClass::Method(...)" (nb: static invocation needs to be added as of 28-Dec-05). So the class in itself provides an implicit namespace.
Remember, making a change is just a matter of resaving the appropriate "ClassName.java" file.
phpsu.com is a free phpscool
TITLE:PHP Hello World Class