PHP Hello World Class

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
 
Line 1: Line 1:
[[Category:PHP]]
+
[[Category:Quercus]]
  
 
==Introduction==
 
==Introduction==

Latest revision as of 17:38, 9 January 2006


Contents

Introduction

Since PHP 3.0, there has been some support for object-oriented programming. The following sample PHP code shows how to:

  • define a class
  • instantiate that class
  • call methods on the instance of the class

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, ");

?>
</body>
</html>

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.

So if you want to modify the java file, all you have to do is re-save it and resin will do the rest.

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%.

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.

Step 1: Create web.xml and place it in %ResinHome%\webapps\ROOT\WEB-INF

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>

Step 2: Create JavaClass.java and place it in %ResinHome%\webapps\ROOT\WEB-INF\classes\example

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/>";
  }

}

Step 3: Create javaclass.php and place it in %ResinHome%\webapps\ROOT

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>

In your favorite browser, type:

http://localhost:8080/javaclass.php

You should see:

Hello, Charles


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.

Personal tools