|
Posted by Henk Verhoeven on 11/05/04 11:32
>>Shailesh<< wrote:
> Hi All,
> can anybody tell me detailed diffrence between PHP and JSP?
> Thanx
> Shailesh
>
Hi Shan,
If i am right there is one difference between php and JSP that really
matters fot how you can build user interfaces: with php the first file
(called the script, the one that is executed by php in reaction to a
http request) is much like a JSP. But if you include another file, it
also acts like a JSP, kind of 'inserting' its output on the place you
called it from.
For example if myScript.php contains:
<HTML><HEAD>
<TITLE>My Page</TITLE>
</HEAD>
<BODY>
<?php include('myBody.inc.php') ?>
</BODY>
</HTML>
this will output the content coming from file 'myBody.inc.php' on the
location of <?php include('myBody.inc.php') ?>. Now i guess you can do
this kind of thing too with JSP. But there is an important difference:
the code from the included file is executed inside the function it is
included from. In the example there is no function, but in the following
example there is:
in 'myScript.php':
<?php
require_once('MyPage.class.php');
$page = new MyPage();
$page->service();
?>
in 'MyPage.class.php':
<?php
class MyPage {
function service() {
include('MyPage.skin.php');
}
function printTitle() {
print "My Page";
}
function printBodyPart() {
require_once('MyBodyPart.class.php');
$part = new MyBodyPart();
$part->service();
}
}
in 'MyPage.skin.php':
<HTML><HEAD>
<TITLE><? $this->printTitle() ?></TITLE>
</HEAD>
<BODY>
<?php $this->printBodyPart() ?>
</BODY>
</HTML>
in 'MyBodyPart.class.php':
class MyBodyPart {
function service() {
include('MyBodyPart.skin.php');
}
function printMessage() {
print "Hello World";
}
}
in 'MyBodyPart.skin.php':
<B><?php $this->printMessage() ?></B>
Becuase the skin files are included inside methods, the $this variable
will reference the object whose method includes the skin. This way we get:
- separation of layout and OOP code,
- pages composed from reusable parts
And all this without a framework, without a template engine, all you
need is to follow a simple pattern! I guess this would not be as simple
with JSP. (Please give us the same example in JSP it i am wrong)
Greetings,
Henk Verhoeven,
www.phpPeanuts.org.
Navigation:
[Reply to this message]
|