Dump Facility in C++ Like Var_Dump() in PHP

Dump facility in C++ like var_dump() in PHP?

There is no such functionality in C++. You can of course write your own Dump() functions. The reason such a feature cannot be generally provided is that the C++ compilation process removes the object metadata needed to structure the dump output. You can of course display structure contents in a debugger, where such metadata is maintained in the debug information.

BTW, are you asking about C or C++? The two languages are quite different, both in features and approach, although neither has var_dump() or similar.

Is there a C++ equivalent to PHP's var_dump and die?

There is no "print a message, then exit" in C or C++. You can quite easily write your own function for die (or panic, as I prefer to call it), something like:

void die(const std::string& msg)
{
std::cerr << msg << std::endl;
exit(1);
}

Or, if you want to avoid the problems with creating a string, which may fail under low memory conditons:

void die(const char *msg)
...

(the rest should be the same as above). The drawback here is that you can't trivially concatenate strings, e.g. die(std::string("Could not open file ") + filename); won't work with const char *.

A function that does var_dump is much harder, as there is no direct way to actually fetch the content of a variable of a an arbitrary type, or an array, in C++. You could perhaps do something like this:

template<typename T>
void var_dump(const T& var)
{
std::cout << var << endl;
}

template<typename T, size_t N>
void var_dump(const T (&var)[N])
{
for(i : var)
{
std::cout << i << endl;
}
}

The latter is a bit like this PHP code: foreach($var as $i) echo $i . "\n";. And I may have the syntax slightly wrong, not sure.

PHP var_dump in C# to dump array or objects?

The closest thing would probably be string.Join:

Console.WriteLine(string.Join(", ", myEnumOfObjects));

It would not automatically include "every property or content of array or object" into the output, though - if you want that to happen, you need to override the ToString method of the object being printed:

class MyObject {
public string Name {get;set;}
public DateTime Dob {get;set;}
public override string ToString() {
return string.Format("{0} - {1}", Name, Dob);
}
}

php var_dump() vs print_r()

The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.

The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.

Example:

$obj = (object) array('qualitypoint', 'technologies', 'India');

var_dump($obj) will display below output in the screen.

object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
}

And, print_r($obj) will display below output in the screen.

stdClass Object ( 
[0] => qualitypoint
[1] => technologies
[2] => India
)

More Info

  • var_dump
  • print_r

Replace var_dump with a custom function in PHP

PHP doesn't support re-declaring functions AFAIK.
However there's a little trick you can do on a case specific basis.

Say you have this code in a file:

 var_dump($a);
var_dump($b);
var_dump($c);

You can just wrap this in a namespace like so:

namespace OverridingGlobalNamespace {
function var_dump($_) {
echo "My custom var_dump";
}

var_dump($a); //Will use namespace function instead of PHP function
var_dump($b);
var_dump($c);
}

How can I capture the result of var_dump to a string?

Use output buffering:

<?php
ob_start();
var_dump($someVar);
$result = ob_get_clean();
?>


Related Topics



Leave a reply



Submit