How to run a php static function from the command line? - php

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

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

How to run Magento shell script in browser

This question has been asked a few times but none of them solves my issue.
I simply created a file:
/shell/test.php
<?php
require_once 'abstract.php';
class Mage_Shell_Test extends Mage_Shell_Abstract
{
public function run() {
echo 'in-shell-test!';
}
}
$shell = new Mage_Shell_Test();
$shell->run();
and commented out $this->_validate(); in
/shell/abstract.php
Then I still see the page of WHOOPS, OUR BAD...
Appreciate any ideas.
You could override the _validate method which is inherited from Mage_Shell_Abstract. This method checks where you are running your script from and it looks like this:
protected function _validate()
{
if (isset($_SERVER['REQUEST_METHOD'])) {
die('This script cannot be run from Browser. This is the shell script.');
}
}
You can change this to return true, but this validation was obviously put there for a reason. If you plan on leaving the script there for a longer period, I would recommend looking for a different approach.

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

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

PHP Static functions don't work when directory changes?

I have a class that looks like this:
utils/Result.php
<?php
class Result
{
public static function ok()
{
echo "OK";
}
}
If I create the following script
./sandbox.php
<?php
require_once("utils/Result.php");
print_r(Result::ok());
And run it with php sandbox.php it works fine. But if I do the following: cd test && php ../sandbox.php it gives me the following error
PHP Fatal error: Call to undefined method Result::ok() in /mnt/hgfs/leapback/sandbox.php on line 5
Now, realize that the require statement seems to be working. If I add a property to the Result class, and use print_r on an instance of it, it looks right. But the static methods disappear. I'm very confused. I'm running php 5.2.6.
Do you have a 'utils/Result.php' file in the directory you have changed to (test)? If yes, it will be included instead of the original file.

Categories