-
Notifications
You must be signed in to change notification settings - Fork 166
Getting started
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 here and run it via your console:
./gradlew run. Note: the first run takes a time. Also use thegradlewcommand instead ofgradleto sample things from this tutorial.
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 {
jcenter()
mavenCentral()
}
sourceSets {
main.resources.srcDirs = ['src']
}
dependencies {
compile 'org.develnext:jphp-core:0.6+' // include jphp with runtime and compiler
compile 'org.develnext:jphp-zend-ext:0.6+' // legacy zend classes and functions
compile 'org.develnext:jphp-json-ext:0.6+' // json support
compile 'org.develnext:jphp-xml-ext:0.6+' // xml library
compile 'org.develnext:jphp-gdx-ext:0.6+' // libgdx wrapper
compile 'org.develnext:jphp-jsoup-ext:0.6+' // library for site parsing in jQuery style
compile 'org.develnext:jphp-mail-ext:0.6+' // for sending mail via smtp
compile 'org.develnext:jphp-webserver-ext:0.6+' // embedded http web server
compile 'org.develnext:jphp-sql-ext:0.6+' // for working with SQL databases, like PDO and JDBC
compile 'org.develnext:jphp-swing-ext:0.6+' // GUI library based on Swing
}
mainClassName = 'php.runtime.launcher.Launcher'
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
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.phpWhy 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.
It's simple:
gradle distZip
The result will be in the build/ directory.
JPHP supports the xdebug protocol for debugging scripts. You need to change a little bit of build.gradle to debug:
apply plugin: 'application'
repositories {
jcenter()
mavenCentral()
}
sourceSets {
main.resources.srcDirs = ['src']
}
dependencies {
compile 'org.develnext:jphp-core:0.6+' // include jphp with runtime and compiler
// .....
compile 'org.develnext:jphp-debugger:0.6+' // 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.
JPHP Group 2015