Skip to content

Instantly share code, notes, and snippets.

@zackad
Last active April 27, 2017 03:08
Show Gist options
  • Save zackad/0670fed334825d7e278329fa478b8c70 to your computer and use it in GitHub Desktop.
Save zackad/0670fed334825d7e278329fa478b8c70 to your computer and use it in GitHub Desktop.
Example calling php command with python
# Example of using python to call external command
# In this case a php command to run php script
from subprocess import call
# First argument is command, second & third argument is parameters
call(["php", "./index.php", "Hello Dunia"])
<?php
namespace Hello;
/**
* Hello World Class
*/
class HelloWorld
{
private $message;
public function __construct()
{
$this->message = 'Hello World!' . PHP_EOL;
}
public function getMessage()
{
return $this->message;
}
public function setMessage($str)
{
if (empty($str)) {
return;
}
$this->message = $str . PHP_EOL;
}
}
<?php
require_once 'HelloWorld.php';
$hello = new Hello\HelloWorld;
// Print initial message
echo $hello->getMessage();
// Set and print new message
if (!empty($argv[1])) {
// if there is message from command line argument
$hello->setMessage($argv[1]);
} else {
$hello->setMessage('Hello again there!');
}
echo $hello->getMessage();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment