call/invoke a method in php class using exec - php

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

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

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 handle a call to an undefined function in a PHP unit test

I am relatively new to unit testing, so maybe someone can help me out here.
Problem
The following error message appears when executing the PHP unit test in the terminal:
Fatal error: Call to undefined function Path\to\missing_function() in /path/to/file.php on line 123
Normally, I would now create a dummy object using the getMock(originalClassName) function and then predefine what should be returned for missing_function() but sadly the function is not placed in any interface/class like all other functions I tested up to now.
Anyone got an idea here? Cheerio!
Generally speaking, you would just include the file with the function and have it get executed. Then you just make sure that after your code was executed, what was supposed to happen happens. You are more concerned with the outcome of the code that you are testing rather than the process of how your code is executed.
Though, if you need to mock a function there is a way using namespaces (need PHP 5.3+). In your test file, you can place a "mock" function that is in the same namespace as your code. When the function gets called php looks in the current namespace first and will find your replacement function. In the normal running of your code, it will proceed to the global namespace to call your function.
So you code would end up like this:
Your class:
namespace Foo;
class SUT {
public function bar() {
return baz();
}
}
Your test:
namespace Foo;
function bar() {
return 'boz';
}
class SUTTest extends PHPUnit_Framework_TestCase {
public function testBar() {
$sut = new SUT();
$this->assertEquals('boz', $sut->bar());
}
}

Categories