◐ Shell
reader mode source ↗
Skip to content
Merged
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
83 changes: 41 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<p align="center"><img src="./docs/img/phpjava.jpg" height="300"></p>

# What is the PHPJava?
The PHPJava is experimental library which emulate JVM (a.k.a. Java Virtual Machine) by PHP 🐘
The PHPJava proceed to read binary from pre-compiled Java file(s) ☕
This project reference to [Java Virtual Machine Specification](https://docs.oracle.com/javase/specs/jvms/se11/html/index.html) documentation when We makes.

We welcoming to contributions this project 💪

## Requirements
- PHP >= 7.2
Expand All @@ -27,12 +27,12 @@ Sorry, I do not have enough time (T_T)
- etc...

## Quick start
- 1) Install the PHPJava into your project.
```
$ composer require memory-agape/php-java
```

- 2) Write Java
```java
class HelloWorld
{
Expand All @@ -43,12 +43,12 @@ class HelloWorld
}
```

- 3) Compile Java
```
$ javac -UTF8 /path/to/HelloWorld.java
```

- 4) Call to main method as following.

```php
<?php
Expand All @@ -65,21 +65,21 @@ use PHPJava\Core\JavaClassFileReader;
);
```

- 5) Get a result
```
$ php /path/to/HelloWorld.php
Hello World
```

## Java Archive (Execute to *.jar file)

- 1) Build your java files to class. Building example is below.
```
$ javac -encoding UTF8 -d build src/*
$ cd build && jar -cvfe ../Test.jar Test *
```

- 2) execute jar on PHPJava with enrtypoint or your targeted method.
```php
<?php
use PHPJava\Core\JavaArchive;
Expand All @@ -98,9 +98,9 @@ use PHPJava\Core\JavaArchive;
);
```

### Get/Set a static fields

- ex) Set or Get a static fields as follows.

```php
<?php
Expand All @@ -119,9 +119,9 @@ $staticFieldAccessor->set('fieldName', 'value');
echo $staticFieldAccessor->get('fieldName');
```

### Call to a static method

- ex) Call to static method as follows.

```php
<?php
Expand Down Expand Up @@ -158,10 +158,10 @@ echo $result;
```


### Get/Set a dynamic fields
If you want to get/set dynamic fields, you need call to `construct` method on Java by PHPJava.

- ex) Call dynamic field as following.

```php
<?php
Expand All @@ -184,10 +184,10 @@ $dynamicFieldAccessor->set('fieldName', 'value');
echo $dynamicFieldAccessor->get('fieldName');
```

### Call to a dynamic method
If you want to get/set dynamic method (same as field), you need call to `construct` method on Java by PHPJava.

- ex) Call dynamic method as following.

```php
<?php
Expand Down @@ -225,7 +225,7 @@ echo $result;

### Output PHPJava operations

- Output debug trace as following if you want to show operated log.

```php
<?php
@@ -247,7 +247,7 @@ $javaClass
$javaClass->debug();
```

- Outputted debug trace is below.

```
[method]
Expand Up @@ -279,22 +279,22 @@ public static void main(java.lang.String[])
---------+--------+----------------------+------------+-----------------
```

- **[method]** is showing called method.
- **[code]** is showing JVM's real programs.
- **[executed]** is showing executed programs.
- **PC** is showing Program Counter.
- **OPCODE** is showing Operation Codes.
- **MNEMONIC** is naming Operation Codes.
- **OPERANDS** is showing stacked items on memory.
- **LOCAL STORAGE** is showing stacked items on a method.

## PHP problems
- **Problem 1:** The PHP is cannot calculating big numbers because of PHP is different to the Java.
But the PHPJava use `bcmath` functions and `gmp` functions to a certain extent to cover to calculate.
The PHPJava return valued is mixed why therefore We recommend to cast to `string` on the PHPJava.

- **Problem 2:** The PHPJava cannot cover to Java's types completely because of PHP is different to the Java.
The Java and the PHPJava comparison table is below.

|Java |PHPJava |
|:-------------:|:-------------:|
Expand All @@ -308,23 +308,22 @@ public static void main(java.lang.String[])
|float |\PHPJava\Kernel\Types\\_Float (including `__toString`), string, float |
|double |\PHPJava\Kernel\Types\\_Char (including `__toString`), string, float |

- **Problem 3:** The PHPJava cannot calculate big numbered `double` and `float` values because of `gmp_pow` cannot calculate negative exponents.
So the PHPJava use built-in functions which is `pow`.

## Run unit tests

- PHPUnit test is below.
```
$ ./vendor/bin/phpunit tests
```

- PHP Coding standards testing is below.

```
$ ./vendor/bin/phpcs --standard=phpcs.xml src
```

- All testing is below.

```
$ composer run tests
Expand All @@ -334,4 +333,4 @@ $ composer run tests
- [Java Virtual Machine Specification](https://docs.oracle.com/javase/specs/jvms/se11/html/index.html)

## License
MIT
Toggle all file notes Toggle all file annotations