Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $this->$cmd($arg) mean in PHP?

Tags:

php

I'm trying to step through some PHP code that I did not write, and I have come to an impasse where I can't figure out how to continue.

I have reached as far as a class member function which looks like this:

public function exec($cmd, $args) {

// ... some argument checks here

$result = $this->$cmd($args);

What is this code doing? The value of $cmd is the string "info" so I assumed that it is calling the member function "info"... but when I put trace code at the top of that function, it is not producing any output.

I have also tried using var_dump($this->$cmd) but it prints NULL. Yet the function is being called and is returning a result, so maybe var_dump can't dump functions?

I have found another answer here which suggests that the above shouldn't work anyway. However, it definitely is assigning a complex value to $result which I am able to dump after the call returns.

If it matters, my trace code is below. I'm looking in the Apache error.log file, and it works fine everywhere else so I'm assuming the trace code is good:

$STDERR = fopen('php://stderr', 'w+');
ob_start();
var_dump($some_variable);
$result999 = ob_get_clean();
fwrite($STDERR, "TRACE: $result999\n");
fclose($STDERR);

Update: I have also tried putting an empty return statement as the first line of the "info" function, but $result still gets assigned the same value as before...

like image 544
Michael Avatar asked Apr 20 '15 23:04

Michael


People also ask

What is argument in PHP?

An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

How do I pass a command line argument in PHP?

To pass command line arguments to the script, we simply put them right after the script name like so... Note that the 0th argument is the name of the PHP script that is run. The rest of the array are the values passed in on the command line. The values are accessed via the $argv array.

What does => mean in PHP?

=> is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user and the value to $pass .

What is argc in PHP?

$argc — The number of arguments passed to script.


1 Answers

As you know, $result and $args are variables.

As you can guess,$this and $cmd are ALSO variables.

$this refers to the class instance. For example, if myClass.a, then "a" can be referenced by any "myClass" method as "$this->a".

PHP, unlike many other object oriented languges, also supports member names as variables. It supports "Variable variables'. That's what $cmd is: whatever value of "cmd" you pass as a parameter, the function expects to find a matching method.

You can find more details here:

http://php.net/manual/en/functions.variable-functions.php

How to explain 'this' keyword in a best and simple way?

As to why you're not hitting your trace statement - that's simply a matter of debugging:

  • Does "$cmd" translate to the name you think it does?
  • Does your class have such a method?
  • Are there any other classes that might be conflicting?
  • Etc

I would fire up your favorite PHP debugger and single-step through the code, if at all possible.

Here are some IDEs that support PHP debugging:

http://www.sitepoint.com/best-php-ide-2014-survey-results/

like image 64
FoggyDay Avatar answered Oct 04 '22 01:10

FoggyDay