PHP Hello World Class

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
Line 48: Line 48:
  
 
Yes... it's that easy.
 
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==
 +
 +
==Step 2: Create JavaClass.java and place it in %ResinHome%\webapps\ROOT\WEB-INF\classes\example==
 +
 +
==Step 3: Create classHello.php and place it in %ResinHome%\webapps\ROOT==
 +
 +
==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.

Revision as of 20:56, 28 December 2005


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

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

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

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