Run PHP File In Terminal/Powershell without .php extension - php

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);
~

Related

calling a php function with exec()

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;
}

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 run a php static function from the command line?

I want to run a function in a php script from the linux command line. The php script looks as follows:
<?php
namespace mycompany\admin;
class MyModel
{
public static function myMethod() {
echo 'something';
}
}
Normally I would do something like this: php thefile.php, but since the function is not called anywhere, it isn't run. I have no idea however, how I could call that function from the command line.
Anybody?
create a file run.php:
<?php
require 'thefile.php';
MyModel::myMethod();
While I would not recommend it, you could call it like this:
php -r "include('thefile.php');mycompany\admin\MyModel::MyMethod();"
You cannot run a PHP method from command line. You need to call it from a script. You could do this:
<?php
namespace mycompany\admin;
class MyModel
{
public static function myMethod() {
echo 'something';
}
}
MyModel::myMethod();
and run it from CLI like so:
php -r myscript.php

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