reflection - Php, how to get parameters as a string (as I got?)

147

example:

$this->method ('1', 2, array(1,2,3), $this);

function method()
{

inmethod(), I want to get parameters just as I got them. I could use this:

foreach (func_get_args() as $item)

but thats not good this time. I simply want to get this string:

'1', 2, array(1,2,3), $this

is there any way?

347

Answer

Solution:

This is an approximation. You could open the code file for read and fetch the row containing the code as well.debug_backtrace() gives you the call stack with the information you need, e.g. code file name, line and name of function call.

<?php
function test()
{
  $trc = debug_backtrace();
  var_dump($trc); //See here what information you can obtain.
  //var_export($trc[0]['args']);
  echo preg_replace('~^\[(.*)]$~', '$1',  json_encode( $trc[0]['args'] ));
}

test('1',2, array(1,2,3), 'some string');
?>    

"1",2,[1,2,3],"some string"

People are also looking for solutions to the problem: php - Doctrine Inheritance and user types

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.