Import java vs include php?

I want to know the difference between include in php and import in java

as well as what is different between the two codes below:

// method 1:

package anyNameOfPackage;
public class Main{
   public class Test{
       public Test(){ //all stuff }
   }
   public static void main(String[] args) {
      // all stuff
   }
}

// method 2:

package anyNameOfPackage;
import otherPackageName.Test;
public class Main{

   public static void main(String[] args) {
      // all stuff
   }
}

- method 2 (which uses import) means that the code is generated between the anyNameOfPackage package and the Main class (for example, include in php) or between the Main class and the main () method (for example, method 1)

Im confused with this import

+4
source share
3 answers

The two directives are completely different.

From the docs :

include . [ ]

Java . Java import .

, Java :

final java.io.File file = ...;

:

import java.io.File;

// and anywhere in the class:
final File file = ...;

Java , " "; . Java Java.

, Java , java.io.File java/io/File.class ( java/io/File.java).

+2

PHP Include include php ( ) ​​.

something.php

<?php
echo 'hello';
?>

otherthing.php

<?php
include "something.php"
echo ', world!';
?>

otherthing.php ,

<?php
echo 'hello';
echo ', world!';
?>

, , , , include, script.

Java

. .

import java.util.ArrayList;
// use ArrayList as if it were locally defined instead of typing java.util.ArrayList
0

Java- Java, . , package PHP namespace, import PHP use as ( use, ). include.

, Java-, :

a/b/c/Class1.java

package a.b.c;

class Class1 {}

d/e/f/Class2.java

package d.e.f;
import a.b.c.Class1;

class Class2 {
    void c2() { 
        Class1 obj1 = new Class1();
    }
}

:

package d.e.f;

class Class2 {
    void c2() { 
        a.b.c.Class1 obj1 = new a.b.c.Class1();
    }
}

, PHP:

Class1.php :

<?php
    namespace a\b\c;

    class Class1 {}

Class2.php:

<?php
    namespace d\e\f;
    use a\b\c\Class1;

    class Class2 {

        function c2() {
            $obj1 = new Class1();
        }
    }

:

<?php
    namespace d\e\f;

    class Class2 {

        function c2() {
            $obj1 = new \a\b\c\Class1();
        }
    }

... autoload include/require Class1.php - auto_prepend_file.

import Class2.java, , Class1 d.e.f.Class1 (, , ). PHP, Class2.php use, , Class1 \d\e\f\Class1.

, PHP include jar , javac Java. , include ( auto_prepend_file) Class1.php PHP Class2, ​​ , Class1.class Class2.java. , , \a\b\c\Class1 a.b.c.Class1, .

include classpath , Java , , , -. - , , , , . - .

  • import PHP use: .
  • java , , PHP include/require, .
  • But PHP include/ requirecan be used to run arbitrary code. There is no equivalent in Java.

References

-1
source

Source: https://habr.com/ru/post/1616438/


All Articles