Skip to main content

Interoperability with PHP

Currently, Scala.php allows running native PHP code from Scala, but not the other way around.

In order to call a PHP-defined function, you have to know its signature. For example, explode:

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

For the example's sake, let's assume we're only interested in providing the first two parameters.

The Scala signature you have to define would be:

import org.scalaphp.php

def explode(
separator: String,
string: String,
): Array[String] = php.native

Currently, PHP 8's named arguments aren't supported. Scala's named parameters still work - just make sure to match the order of arguments when defining the signature!

Having defined the signature, we can call the function:

explode(string = "foo bar", separator = " ") // Array("foo", "bar")