calling a php function with exec() - php

I am trying to use exec() to run and pass parameters to a php function named new_array() in array_module.php and expect an array return but having no luck. using the format:
$cmd = exec('cd "c:/wamp/www/test_array" && php array_module.php "'.$input['first'].'"
"'.$input['second'].'" ');
Any help is appreciated

Don't make a call through the command line to another file. Import the other file so its functions become available and simply call the function directly:
require_once 'c:/wamp/www/test_array/array_module.php';
$result = new_array($input['first'], $input['second']);
This assumes that array_module.php is written in a sane way so it can be imported elsewhere of course, e.g.:
<?php
function new_array($one, $two) {
...
return $result;
}

Related

Run PHP File In Terminal/Powershell without .php extension

I've seen numerous posts of running php files without extensions on the browser.My question is,is it possible to do the same when running a file in cli?Example scenario:I have a file called Test.php
that contains this simple code:
class Test
{
public function action()
{
global $argv;
$script = array_shift($argv);
print($argv[0]);
}
}
(new Test)->action();
Now on the terminal,instead of doing
$ php Test.php -calltoAction
I'd like to do:
$ php Test -calltoAction
and get calltoAction printed out.How do I accomplish this.
It is certainly possible. PHP CLI does not care about the file extensions.
Trying to pass argv to your function as a global is not the way to go. Pass it as a function argument instead.
class Test
{
public function action($args)
{
$script = array_shift($args);
print($args[0]);
}
}
(new Test)->action($argv);
~

Getting current method name inside include/require

I currently have this PHP code;
private function generateSpecialPage(){
require_once("/view/pages/special.php");
}
Special.php is a php file mostly filled with html. I'm trying to obtain the name of the current function from inside special.php.
If I echo the magic constant FUNCTION before the require, it echoes "generateSpecialPage", which is what I want. However, if I echo FUNCTION from special.php, it echoes nothing.
I'm able to get the current class' name from inside special.php using get_class($this), I was wondering if there was an equally elegant solution for the current method.
You should probably re-organize your code to change require_once("/view/pages/special.php") into a separate function and pass in the function name.
If you call generateSpecialPage() a second time, it won't do anything. You could get around this by changing it to require(), but then you're loading the file every time which is unnecessary.
The required file lives in the same scope as the function that required it.
So you can simply store to a variable before requiring the file:
private function generateSpecialPage() {
$caller = __FUNCTION__;
require_once '/view/pages/special.php';
}
then in special.php you have a regular variable $caller:
<?= "required by {$caller}" ?>
A general purpose call stack inspector will let you look back to fetch the caller function, class or object, file, and line. The one I use as part of my framework looks like, in essence:
function caller($offset = 0) {
return (new \Exception)->getTrace()[1+$offset];
}
Using this in your special.php will yield the desired result:
<?php echo caller(1)['function']; ?>
The call stack at that point is special.php -> require -> doSomethingSpecial, so we use the 1 offset to skip passed the require and get the doSomethingSpecial frame.
However, you might consider refactoring your view to receive parameters, rather than taking environmental cues. A general purpose view loader would go something like this:
function render($template, array $params = []) {
extract($params);
require $template;
}
which would then have a template that looked liked:
<?php echo "Hello {$caller}" ?>
and could be called like:
private function doSomethingSpecial() {
render('special.php', [ 'caller' => __FUNCTION__ ]);
}
I realize this is more typing than might be desired, but it affords more flexibility in the long-term, as it decouples the view from the caller.

Does PHP has python execfile like function?

Python has a function where we can execute a other python file and get methods from that file in vars. Below is the sample rough code to explain:=
file1.py
def method1():
print 'hello world'
file2.py
globals = file1.__dict__
execfile(file1.py, globals, locals);
# locals['method1'] has method up from file1.py. One can even execute it by doing locals['method1']();
I want similar method in PHP, where I can read other PHP file and get methods in a variable. Is this even possible
You can define class in source PHP organized in namespace
eg:
source code
vendor\vendorname\helpers.php
<?php
namespace vendor\vendorname\helpers;
class TestHelper
{
public static function yourClassFunc ($param)
{
/** your code **/
}
}
and use where you need simply declarinng
other php source (eg atest.hp)
use vendor\dfenx\helpers\UIHelper;
...........
echo UIHelper::yourClassFunc( $param);

How to refer to a function?

I am learning how to use hooks in mediawiki. I am also new to PHP.
General hook handler can be added by putting next line in to LocalSettings.php:
$wgHooks['event'][] = 'function';
Suppose I wrote myfunction in my.php file. How can I point to this function from LocalSettings.php
Edit. I have written some function in my.php file. How to refer to this function?
I should tell mediawiki where find this function.I don't know how to do it.
Should I write '$wgHooks['event'][] = 'my.php:function'. Or I should include my.php file to LocalSetting and then just write '$wgHooks['event'][] = 'function'
As the docs say, you need to push a string with your function name (or an array of strings etc.) to the hook array.
AFAIK, when triggering the hook they will be invoked with call_user_func(). So, it will depend on your function declaration in the my.php file. With a myfunction, it should be
$wgHooks['event'][] = 'myfunction';
from what i read from the mediawiki docs, you need to create an extension, and in your extension you install your hook. in my.php you will write:
// $wgHooks is a global variable
$wgHooks['event'][] = 'function';
Hope i understand correctly
Extension docs
http://www.mediawiki.org/wiki/Manual:Extensions
I am not sure I understand completely what you want, but if you want to call a user defined function you can use call_user_func which takes as argument the name of your function. You have to include the file so the function will be available.
call_user_func('myfunction ', array());
You refer to functions via callbacks in PHP. A callback is one of these:
a function name: 'myFunc'
an array containing a class name and a function name: array('MyClass', 'myFunc')
an array containing an object and a function name: array($myObj, 'myFunc')
an anonymous function (which is technically a Closure object): function($x, $y) { /* PHP code */ } (this is PHP 5.3+ only, but so are recent versions of MediaWiki)
Invoking these callbacks via call_user_func($callback, $arg1, $arg2) will be equivalent to the following, respectively:
myFunc($arg1, $arg2);
MyClass::myFunc($arg1, $arg2);
$myObj->myFunc($arg1, $arg2);
executing the body of the anonymous function, replacing $x and $y with $arg1 and $arg2
If this code would fail (e.g. you use the first version, and the function myFunc is not loaded), the callback will also fail. You can use autoloading with the second form, in MediaWiki that is normally done via $wgAutoloadClasses:
// in MyExtension.php
$wgHooks['event'][] = array('MyExtension', 'myEventHandler');
$wgAutoloadClasses['MyExtension'] = dirname(__FILE__) . 'MyExtension.body.php';
// in MyExtension.body.php
class MyExtension {
public function myEventHandler($p1, $p2) {
// do stuff
}
}
That way, you can load MyExtension.php (which is a small file with configuration settings only) from LocalSettings.php, and MyExtension.body.php (which has all the code) only gets loaded in those requests which actually use your extension.

call/invoke a method in php class using exec

In php suppose there is a class named as 'test_class' in test.php with two methods: method_1 and method_2.
This is my test.php file:
class test_class
{
function method_1
{ ....... }
function method_2
{ ....... }
}
Now, using the "exec" command from other php file, I want to run only the method_1 in the test_class.
I want to know how this can be achieved?
You can do this instead:
include_once 'test.php'; // correct path if needed
$test = new test_class();
// call method1
$test->method_1();
Have you read the manual?
exec isn't meant to call methods, it's used to:
exec — Execute an external program

Categories