简体   繁体   中英

Difference between PHP includes and Java Import Declarations

In PHP, we have:

<?php include 'external_file.php'; ?>

Whereas in Java, you have imports:

import javax.servlet.http.HttpServlet;

It is to my understanding that PHP includes simply dump the contents of the external file into the file that contains the include statement.

My gut feeling is that Java handles these includes/imports differently than PHP. What are the key differences?

PHP's include is pretty much the exact same thing as having literally cut/pasted the raw contents of the included file at the point where the include() directive is.

Java's compiled, so there's no source code to "include" - the JVM is simply loading object/class definitions and making them available for use. It's much like the #include directives in C. you're not loading literal source code, just function definitions/prototypes/fingerprints for later use.

In php it simply dumps the contents of the file in the current file. In Java, an imported class is used:

  1. For compiling the source to byte code using the imported classes.
  2. At runtime when the JVM sees that your program references the imported class, it loads it and uses it(for method invocations and member accesses if it is the case)

PHP simply just includes whatever is in that file. It's simply merging the two files together.

Java's import function gives you access to the methods specified in that import. Basically, PHP is just a rudimentary combining of the two files while Java gives you access to that file's methods and interface.

They are very different. Php just include the source code from the included file. Java is using the ClassLoader to load the compiled class located somewhere in the CLASSPATH. The import just tells the compiler that you want to reference those classes in the current namespace. The import does not load anything by itself, only when you use new , the JVM will load the class.

You have <jsp:include> in Java similar to PHP include. Java import is similar to PHP load module.

The closest to a php include in Java is a static import. Ie something like: import static javax.servlet.http.HttpServlet . This allows you to reference methods in the same class file as if they were declared locally (this only applies for static members of the imported class. However, this is very seldom used. It's a tighter form of coupling and should be avoided in most cases. The only time I find it helpful is for Junit test cases. Doing a static import of org.junit.Assert allows you to use the shorter form assertEquals(...) instead of Assert.assertEquals(...). Check out Oracle's documentation on static imports here .

The main difference from my experience is that PHP allows you do do anything. You can treat PHP includes the same way as Java uses its imports. A PHP file can be all function, or it can simply execute from start to finish.

So your php file could be

<?php

echo(1 + 4)

?>

or it could include function which you call later on

<?php

function addTwoNumbers()
{
return 1 + 4;
}
?>

If you inccluded the second php file you could call the addTwoNumbers function below your include statement. I like to practice specifying individual functions rather than create many PHP files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM