Skip to content

Getting started

Dmitriy Zayceff edited this page Oct 22, 2017 · 33 revisions

This is a simple way to try JPHP.

Before we start, you need to download and install the Gradle distributive. Do not forget to add the gradle bin path to your PATH variable.


If you have not enough experience, you can download the project from this tutorial (see below) and run it via your console: ./gradlew run. Note: the first run takes a time. Also use the gradlew command instead of gradle to sample things from this tutorial.

Download Get-Started.zip


1. Create the next directories and files:

build.gradle
src/
   JPHP-INF/
      .bootstrap.php
      launcher.conf

2. Change the gradle build file - build.gradle:

apply plugin: 'application'

repositories {
    maven { url "https://jitpack.io" }
    mavenCentral()
}

sourceSets {
    main.resources.srcDirs = ['src']
}

def version = '0.9.1'

dependencies {
    compile "org.develnext.jphp:jphp-core:${version}" // include jphp with runtime and compiler

    // optional
    compile "org.develnext.jphp:jphp-zend-ext:${version}" // legacy zend classes and functions
    compile "org.develnext.jphp:jphp-json-ext:${version}" // json support
    compile "org.develnext.jphp:jphp-xml-ext:${version}" // xml library
    compile "org.develnext.jphp:jphp-jsoup-ext:${version}" // library for site parsing in jQuery style
    compile "org.develnext.jphp:jphp-mail-ext:${version}" // for sending mail via smtp
    compile "org.develnext.jphp:jphp-sql-ext:${version}" // for working with SQL databases, like PDO and JDBC
    compile "org.develnext.jphp:jphp-git-ext:${version}" // for working with Git repositories

    compile "org.develnext.jphp:jphp-debugger:${version}" // JPHP debugger (xdebug)
}

task wrapper(type: Wrapper) {
    gradleVersion = '3.4'
}

run {
    mainClassName = 'php.runtime.launcher.Launcher'
    jvmArgs += ["-Dfile.encoding=UTF-8"]
}

task debug(dependsOn: run.dependsOn) {
    doFirst {
        run.jvmArgs += ["-Djphp.debug=true", "-Djphp.config=debug.conf"]
        run.execute()
    }
}

3. Write any php code in the JPHP-INF/.bootstrap.php:

<?php echo "Hello World";

4. Use the command line to run your app:

gradle run

What about class loading?

By default Launcher uses a special class loader to load your classes from the src directory, you still can use namespaces, for example - my\pack\AnyClass will be loaded from the src/my/pack/AnyClass.php file automatically. You still can register your class loader via spl_register_autoload in src/JPHP-INF/.bootstrap.php and disable the default class autoloader via the env.classLoader option in launcher.conf:

env.classLoader = 

If you want to require or include scripts from resources (classpath), you should use the res:// protocol:

include 'res://Bootstrap.php'; // include src/Bootstrap.php

Why you cannot include src/Bootstrap.php or Bootstrap.php? You should load your sources from class path directories because when you will want to create a jar file of your project with php sources - you will can it. The result jar file will contain the all sources inside.

How to create a distributive of your project?

It's simple:

gradle distZip

The result will be in the build/ directory.

How to debug my app via XDebug?

JPHP supports the xdebug protocol for debugging scripts. You need to change a little bit of build.gradle to debug:

 apply plugin: 'application'

repositories {
    maven { url "https://jitpack.io" }
    mavenCentral()
}

sourceSets {
    main.resources.srcDirs = ['src']
}

dependencies {
    compile 'org.develnext.jphp:jphp-core:0.8+' // include jphp with runtime and compiler
// .....
    compile 'org.develnext.jphp:jphp-debugger:0.8+' // add debugger
}

// Add new lines:
run {
    mainClassName = 'php.runtime.launcher.Launcher'
    jvmArgs += ["-Dfile.encoding=UTF-8"]
}

task debug(dependsOn: run.dependsOn) {
    doFirst {
        run.jvmArgs += ["-Djphp.debug=true"]
        run.execute()
    }
}

After this, you can run your app via gradle debug to debug. You need to add a XDebug remote configuration in your IDE with parameters: port = 9000, host: localhost, remote port = 0.

Clone this wiki locally