Giter Club home page Giter Club logo

php4java's Introduction

php4java

PHP4JAVA is a java-library, that integrates PHP-interpreter into Java and allows to run PHP code directly from JAVA.
This repository is based on very old semi-working code from https://github.com/adsr/php4j
Now it's a Gradle-project that can be used as subproject in any other Gradle-project.

WARNING: for now only Mac OS X is supported!

How to use in java

  1. Create PHP-instance var php = php4java.PhpInstanceFactory.CreateInstance(); - this returns IPhp (PHP instance interface).
    Now you have separate PHP instance with it's own cURL, OpenSSL and other statically linked extensions.
  2. Execute any PHP-code
  • php.execString("include \"myScript.php\""); - this includes and executes myScript.php.
  • var result = php.execString("return $a + $b;") - this returns result of $a + $b in IPhpVal (PHP value interface).
  1. Use returned value
  • String text = php.execString("return 10;").asString(); - returns String "10".
  • Long value = php.execString("return 10;").asLong(); - returns Long value 10.
  • Boolean value = php.execString("return 10;").asBoolean(); - returns Boolean value true.
  • and others...

Is it a single PHP instance for Java?

No. Each call php4java.PhpInstanceFactory.CreateInstance(); creates new independent PHP copy with it's own variable pool and own extensions copies. If one PHP crashes, other instances still can work.
Even better: each PHP-instance is thread-safe! You can use each instance in different threads simultaneously, it's ok.

How does php.execString(...) work?

It uses PHP's eval(...) to execute commands. It's not good when you use eval(...) in PHP-code, but for this project it's almost the only option.
For example, if you write php.execString("abcdefg"), PHP executes this: eval('abcdefg').
Warning: it's. very dangerous, be careful! Do not use symbols like ' or make escape sequences like this: php.execString("return \\'abcdefg\\';") => eval('return \'abcdefg\';').

What if PHP crashes or throws exception (or we have syntax errors in our expression/php-file)?

PHP-instance's execString(...) throws Php4JavaException with message of your PHP-exception (including file name and line, where exception was thrown) or with info, that exception was unknown (for example, if PHP's Zend engine fails or anything else).

Example:

var php = php4java.PhpInstanceFactory.CreateInstance();
try
{
    var result = php.execString("Here we do something wrong!");
}
catch (php4java.Php4JavaException exception)
{
    System.out.println(exception.getMessage());
}

What do I need to build?

  • Homebrew
  • OpenJDK 11 or higher
  • Gradle
  • autoconf 2.68 or higher (brew install autoconf)
  • automake 1.7 or higher (brew install automake)
  • libtoolize 1.4.2 or higher (brew install libtool)
  • Bison 3.0.0 or higher (brew install bison)
  • re2c 0.13.4 or higher (brew install re2c)
  • gxargs (brew install findutils)
  • pkg-config (brew install pkg-config)
  • libiconv (brew install libiconv)
  • oniguruma (brew install oniguruma)
  • libxml2 (brew install libxml2)
  • nghttp2 (brew install nghttp2)
  • libidn2 (brew install libidn2)
  • rtmpdump (brew install rtmpdump)
  • brotli (brew install brotli)

How to build?

To build just run gradle build in base project directory

php4java's People

Contributors

storyboardcreativity avatar adsr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.