Skip to main content

Java to php converter

If you are try to convert your java code into equivalent php so that is right place. This tool is capable to transpile some java 8 package with limited functionality. This is an experimental version.

Method overloading example

Java Code
public class Repeat
{
    int a;
    int b;
    Repeat(int a, int b)
    {
        this.a = a;
        this.b = b;
    }
    Repeat()
    {}
    Repeat(int a)
    {
        this.a = 1;
        this.b = 10;
    }
    public void display()
    {
        System.out.println("a:" + a);
        System.out.println("b:" + b);
    }
    public void test()
    {
        System.out.println("a:" + this.a);
    }
    public void test(int c)
    {
        System.out.println("a : " + a + " , b : " + b + " c : " + c);
    }
    public static void main(String[] aa)
    {
        Repeat t = new Repeat();
        Repeat t3 = new Repeat(44);
        t.display();
        t.test();
        t.test(2);
        t3.test(3);
    }
}
Php Code
<?php

class Repeat
{
    function __construct(){
        $this->a = 0;
        $this->b = 0;
    }
    public $a;
    public $b;
    public static function Repeat_1($a, $b)
    {
        $local_this = new Repeat();
        $local_this->a = $a;
        $local_this->b = $b;
        return $local_this;
    }
    public static function Repeat_2()
    {
        $local_this = new Repeat();
        return $local_this;
    }
    public static function Repeat_3($a)
    {
        $local_this = new Repeat();
        $local_this->a = 1;
        $local_this->b = 10;
        return $local_this;
    }
    public function display()
    {
        echo "a:" . strval($this->a),"\n";
        echo "b:" . strval($this->b),"\n";
    }
    public function test_1()
    {
        echo "a:" . strval($this->a),"\n";
    }
    public function test_2($c)
    {
        echo "a : " . strval($this->a) . " , b : " . strval($this->b) . " c : " . strval($c),"\n";
    }
    public static function main(&$aa)
    {
        $t = Repeat::Repeat_2();
        $t3 = Repeat::Repeat_3(44);
        $t->display();
        $t->test_1();
        $t->test_2(2);
        $t3->test_2(3);
    }
}
Repeat::main($argv);

Array example

// Test Array
public class Task
{
    public static void main(String[] aa)
    {
        int[] a = new int[10];
        int[][] b = {
            {
                1 , 2
            },
            {
                3 , 5
            }
        };
        System.out.println(a.length);
        System.out.println(b.length);
    }
}
Php Code
<?php

// Test Array
class Task
{
    function __construct(){
    }
    public static function Task()
    {
        $local_this = new Task();
        return $local_this;
    }
    public static function main(&$aa)
    {
        $a = array_fill(0,10,0);
        $b = array(
            array(
                1, 2
            ), array(
                3, 5
            )
        );
        printf("%d\n",count($a));
        printf("%d\n",count($b));
    }
}
Task::main($argv);

Recursion Example

///Fibonacci Series in reverse order
// by using recursion
public class FibonacciSeries
{
	static void fib(int counter, int initial, int next)
	{
		//base condition
		if (counter > 0)
		{
			//Call fib function recursively
			fib(counter - 1, next, next + initial);
			//Display series element
			System.out.print("  " + initial);
		}
	}
	public static void main(String[] args)
	{
		int counter = 6;
		System.out.print("Fibonacci series of " + counter + " Elements  :");
		fib(counter, 0, 1); //Call fab method
		counter = 12;
		System.out.print("\nFibonacci series of " + counter + " Elements :");
		fib(counter, 0, 1); //Call fab method
	}
}
Php Code
<?php

// /Fibonacci Series in reverse order
// by using recursion
class FibonacciSeries
{
    function __construct(){
    }
    public static function FibonacciSeries()
    {
        $local_this = new FibonacciSeries();
        return $local_this;
    }
    static function fib($counter, $initial, $next)
    {
        // base condition
        if ($counter > 0)
        {
            // Call fib function recursively
            FibonacciSeries::fib($counter - 1, $next, $next + $initial);
            // Display series element
            echo "  " . strval($initial);
        }
    }
    public static function main(&$args)
    {
        $counter = 6;
        echo "Fibonacci series of " . strval($counter) . " Elements  :";
        FibonacciSeries::fib($counter, 0, 1);
        // Call fab method
        $counter = 12;
        echo "\nFibonacci series of " . strval($counter) . " Elements :";
        FibonacciSeries::fib($counter, 0, 1);
    }
}
FibonacciSeries::main($argv);




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment






Steven C     37 Day ago
Are you able to use this online java to php converter to convert the whole encog-java-core project to PHP so I can use the encog ML library in PHP ?
The java source is here: https://github.com/jeffheaton/encog-java-core/archive/refs/tags/v3.3.zip
I tried this but it errors out: https://github.com/bafolts/java2php
This lady was working on it but abandoned it in 2015: https://github.com/katrinaniolet/encog-php-core

Thanks, this would be really helpful, since none of the PHP ML libraries allow multidimensional array data for training, but Encog does if the library can be ported to PHP.