Call a PHP method from the command line
Assuming you have a class and a method like this:
<?php
namespace Some\Name\Space;
class MyExistingClass
{
// ...
/**
* This is a sample method
* @param string $param1
* @param string $param2
*/
public function someExistingMethod(string $param1, ?string $param2 = null)
{
// Your code
}
// ...
}
You can run this method from the command line with the following command:
scriptify run \
"\\Some\\Name\\Space\\MyExistingClass::someExistingMethod" \
--arg value1 \
--arg value2
Naming the class without fighting the shell
A backslash is the shell's escape character, which is why the form above is quoted and doubled. Scriptify also accepts forward slashes as namespace separators, and they need no escaping at all — anywhere:
scriptify run Some/Name/Space/MyExistingClass::someExistingMethod \
--arg value1 \
--arg value2
The two spell the same class. A forward slash is not valid in a PHP identifier or namespace, so there is nothing for it to be confused with, and Scriptify converts it before anything else happens — a service installed this way still records the canonical backslash form.
This is worth knowing wherever the command passes through another layer of quoting. In
a composer.json script, for example, each backslash is escaped once by JSON and again
by the shell, so a single separator becomes four characters:
"revalidate": "vendor/bin/scriptify run \"\\\\Some\\\\Name\\\\Space\\\\MyClass::method\""
against:
"revalidate": "vendor/bin/scriptify run Some/Name/Space/MyClass::method"
If you prefer backslashes, use single quotes in the shell: they keep the backslash literal, so one per separator is enough.
scriptify run '\Some\Name\Space\MyExistingClass::someExistingMethod'
--rootdir is the directory to run in, and --bootstrap a file relative to it: the
autoload file (vendor/autoload.php by default), or a file that returns your PSR-11
container, to have the class built by it (see container).
scriptify run \
"\\Some\\Name\\Space\\MyExistingClass::someExistingMethod" \
--bootstrap "vendor/autoload.php" \
--rootdir "/path/to/root" \
--arg value1 \
--arg value2