By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

PHP Catchable fatal error: Object of class phpEnv could not be converted to string

P: n/a

I am a newcomer to using PHP but not to programming (C, C++, Javascript).

I am playing around with classes and wanted to make a function that has a
method simply for producing either plain text or HTML output in a tabular
way a listing of the global variables in the [key] = keyValue two-column
format. That's what the method listGlobals does.

I am stumped by the following error message:

PHP Catchable fatal error: Object of class phpEnv could not be
converted to string in <URL-hereon line 43

There are really few results from Google searches, with results usually
having people ask the same questions with no answers.

Comments on what is causing the error and general tips on class
construction are welcome.

===

The contents of file EnvClass.php are shown below, and line 43 is commented
(don't count lines in text below...code was re-formatted for posting):

PHP Version 5.2.6

-----------------start of file EnvClass.php <---------------------------
<?php

class phpEnv {
public $superglobals;
public $superglobalNames;

function phpEnv ()
{
$this->superglobals = array($GLOBALS, $_SERVER, $_GET, $_POST,
$_FILES, $_REQUEST, $_ENV, $_COOKIE);
$this->superglobalNames = array('$GLOBALS', '$_SERVER', '$_GET',
'$_POST', '$_FILES', '$_REQUEST', '$_ENV', '$_COOKIE');
if (isset($_SESSION) == true)
{
array_push($superglobals, $_SESSION);
array_push($superglobalNames, '$_SESSION');
}
}

function listGlobals ($inHTML=false, $tableID=null, $keyStyle=null,
$valueStyle=null, $headingStyle=null)
{
$output = "";
if ($inHTML == true)
$output .= "<table id='".$tableID."' class='report'>";
for ($i = 0; $i < count($this->superglobals); $i++)
{
if ($inHTML == true)
{
$output .= "<tr><th colspan=\"2\" class=\"header\"";
if (isset($headingStyle) == true)
$output .= " style=\"".$headingStyle."\"";
$output .= ">".$this->superglobalNames[$i]."</th></tr>\n";
}
else
$output .= $this->superglobalNames[$i];
foreach ($this->superglobals[$i] as $key =$value)
if ($inHTML == true)
{
$output .= "<tr><td class=\"key\"";
if (isset($keyStyle) == true)
$output .= " style=\"".$keyStyle."\"";
$output .= ">".$this->superglobalNames[$i].
"[".$key."] =&gt;</td>\n";
$output .= "<td class=\"value\"";
if (isset($valueStyle) == true)
$output .= " style=\"".$valueStyle."\"";
/************** LINE 43 is line immediately below *********************/
$output .= '>'.$value.'</td></tr>\n';
}
else
$output .= $this->superglobalNames[$i].
"[".$key."] == ".$value."\n";
}
if ($inHTML == true)
$output .= "</table>";
else
$output .= "\n\n";
return ($output);
}
}

?>
-----------------end of file EnvClass.php <---------------------------
Aug 24 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
..oO(Patient Guy)
>I am a newcomer to using PHP but not to programming (C, C++, Javascript).

I am playing around with classes and wanted to make a function that has a
method simply for producing either plain text or HTML output in a tabular
way a listing of the global variables in the [key] = keyValue two-column
format. That's what the method listGlobals does.

I am stumped by the following error message:

PHP Catchable fatal error: Object of class phpEnv could not be
converted to string in <URL-hereon line 43

There are really few results from Google searches, with results usually
having people ask the same questions with no answers.

Comments on what is causing the error and general tips on class
construction are welcome.

===

The contents of file EnvClass.php are shown below, and line 43 is commented
(don't count lines in text below...code was re-formatted for posting):

PHP Version 5.2.6

-----------------start of file EnvClass.php <---------------------------
<?php

class phpEnv {
public $superglobals;
public $superglobalNames;

function phpEnv ()
The constructor should be called __construct(). You're using the old and
deprecated PHP 4 naming.
>[...]
if ($inHTML == true)
The "== true" is not really necessary if you test an expression which
already is or evaluates to a boolean.
>[...]
foreach ($this->superglobals[$i] as $key =$value)
if ($inHTML == true)
{
$output .= "<tr><td class=\"key\"";
if (isset($keyStyle) == true)
$output .= " style=\"".$keyStyle."\"";
$output .= ">".$this->superglobalNames[$i].
"[".$key."] =&gt;</td>\n";
$output .= "<td class=\"value\"";
if (isset($valueStyle) == true)
$output .= " style=\"".$valueStyle."\"";
/************** LINE 43 is line immediately below *********************/
$output .= '>'.$value.'</td></tr>\n';
You're looping through all the superglobals, but the $GLOBALS array also
contains the object of the phpEnv class, which runs this code. If you
try to print it out, you'll get the error message.

Also watch for recursion. The $GLOBALS array contains itself and all the
other superglobals as well!
>[...]
$output .= $this->superglobalNames[$i].
"[".$key."] == ".$value."\n";
You should use interpolation or sprintf() to keep the code more readable
and to avoid the ugly and sometimes error-prone concatenation, e.g.

$output .= "{$this->superglobalNames[$i]}[$key] ==$value\n";

or (which is what I usually prefer)

$output .= sprintf("%s[%s] ==%s\n",
$this->superglobalNames[$i],
$key,
$value
);

The sprintf() way also makes it much easier to apply functions like
htmlspecialchars(), which should always be used when printing out
arbitrary values to an HTML page.

Micha
Aug 24 '08 #2

P: n/a
Patient Guy wrote:
I am a newcomer to using PHP but not to programming (C, C++, Javascript).

I am playing around with classes and wanted to make a function that has a
method simply for producing either plain text or HTML output in a tabular
way a listing of the global variables in the [key] = keyValue two-column
format. That's what the method listGlobals does.

I am stumped by the following error message:

PHP Catchable fatal error: Object of class phpEnv could not be
converted to string in <URL-hereon line 43

There are really few results from Google searches, with results usually
having people ask the same questions with no answers.

Comments on what is causing the error and general tips on class
construction are welcome.

===

The contents of file EnvClass.php are shown below, and line 43 is commented
(don't count lines in text below...code was re-formatted for posting):

PHP Version 5.2.6

-----------------start of file EnvClass.php <---------------------------
<?php

class phpEnv {
public $superglobals;
public $superglobalNames;

function phpEnv ()
{
$this->superglobals = array($GLOBALS, $_SERVER, $_GET, $_POST,
$_FILES, $_REQUEST, $_ENV, $_COOKIE);
$this->superglobalNames = array('$GLOBALS', '$_SERVER', '$_GET',
'$_POST', '$_FILES', '$_REQUEST', '$_ENV', '$_COOKIE');
if (isset($_SESSION) == true)
{
array_push($superglobals, $_SESSION);
array_push($superglobalNames, '$_SESSION');
}
}

function listGlobals ($inHTML=false, $tableID=null, $keyStyle=null,
$valueStyle=null, $headingStyle=null)
{
$output = "";
if ($inHTML == true)
$output .= "<table id='".$tableID."' class='report'>";
for ($i = 0; $i < count($this->superglobals); $i++)
{
if ($inHTML == true)
{
$output .= "<tr><th colspan=\"2\" class=\"header\"";
if (isset($headingStyle) == true)
$output .= " style=\"".$headingStyle."\"";
$output .= ">".$this->superglobalNames[$i]."</th></tr>\n";
}
else
$output .= $this->superglobalNames[$i];
foreach ($this->superglobals[$i] as $key =$value)
if ($inHTML == true)
{
$output .= "<tr><td class=\"key\"";
if (isset($keyStyle) == true)
$output .= " style=\"".$keyStyle."\"";
$output .= ">".$this->superglobalNames[$i].
"[".$key."] =&gt;</td>\n";
$output .= "<td class=\"value\"";
if (isset($valueStyle) == true)
$output .= " style=\"".$valueStyle."\"";
/************** LINE 43 is line immediately below *********************/
$output .= '>'.$value.'</td></tr>\n';
}
else
$output .= $this->superglobalNames[$i].
"[".$key."] == ".$value."\n";
}
if ($inHTML == true)
$output .= "</table>";
else
$output .= "\n\n";
return ($output);
}
}

?>
-----------------end of file EnvClass.php <---------------------------
$superglobals[$i] (which is aliased as $value in your foreach loop) is
an array, not a simple type. You can't string concatenate arrays.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 24 '08 #3

P: n/a
Jerry Stuckle <js*******@attglobal.netwrote in comp.lang.php:
Patient Guy wrote:
>I am a newcomer to using PHP but not to programming (C, C++,
Javascript).

I am playing around with classes and wanted to make a function that
has a method simply for producing either plain text or HTML output in
a tabular way a listing of the global variables in the [key] =
keyValue two-column format. That's what the method listGlobals does.

I am stumped by the following error message:

PHP Catchable fatal error: Object of class phpEnv could not be
converted to string in <URL-hereon line 43

There are really few results from Google searches, with results
usually having people ask the same questions with no answers.

Comments on what is causing the error and general tips on class
construction are welcome.

===

The contents of file EnvClass.php are shown below, and line 43 is
commented (don't count lines in text below...code was re-formatted
for posting):

PHP Version 5.2.6

-----------------start of file EnvClass.php
<--------------------------- <?php

class phpEnv {
public $superglobals;
public $superglobalNames;

function phpEnv ()
{
$this->superglobals = array($GLOBALS, $_SERVER, $_GET, $_POST,
$_FILES, $_REQUEST, $_ENV, $_COOKIE);
$this->superglobalNames = array('$GLOBALS', '$_SERVER',
'$_GET',
'$_POST', '$_FILES', '$_REQUEST', '$_ENV', '$_COOKIE');
if (isset($_SESSION) == true)
{
array_push($superglobals, $_SESSION);
array_push($superglobalNames, '$_SESSION');
}
}

function listGlobals ($inHTML=false, $tableID=null,
$keyStyle=null,
$valueStyle=null, $headingStyle=null)
{
$output = "";
if ($inHTML == true)
$output .= "<table id='".$tableID."' class='report'>";
for ($i = 0; $i < count($this->superglobals); $i++)
{
if ($inHTML == true)
{
$output .= "<tr><th colspan=\"2\" class=\"header\"";
if (isset($headingStyle) == true)
$output .= " style=\"".$headingStyle."\"";
$output .=
">".$this->superglobalNames[$i]."</th></tr>\n";
}
else
$output .= $this->superglobalNames[$i];
foreach ($this->superglobals[$i] as $key =$value)
if ($inHTML == true)
{
$output .= "<tr><td class=\"key\"";
if (isset($keyStyle) == true)
$output .= " style=\"".$keyStyle."\"";
$output .= ">".$this->superglobalNames[$i].
"[".$key."] =&gt;</td>\n";
$output .= "<td class=\"value\"";
if (isset($valueStyle) == true)
$output .= " style=\"".$valueStyle."\"";
/************** LINE 43 is line immediately below
*********************/
$output .= '>'.$value.'</td></tr>\n';
}
else
$output .= $this->superglobalNames[$i].
"[".$key."] == ".$value."\n";
}
if ($inHTML == true)
$output .= "</table>";
else
$output .= "\n\n";
return ($output);
}
}

?>
-----------------end of file EnvClass.php
<---------------------------

$superglobals[$i] (which is aliased as $value in your foreach loop) is
an array, not a simple type. You can't string concatenate arrays.
Within the foreach loop, doesn't $this->superglobals[$i] reference a
single PHP-predefined global variable like $_SERVER and the like?

That is, with this code:

foreach ($this->superglobals[$i] as $key =$value)

I am basically saying, as the $i counter is incremented:

foreach ($_SERVER as $key =$value)
foreach ($_ENV as $key =$value)
foreach ($_REQUEST as $key =$value)
and so on

Is this not so?

I am assuming that because they are associative arrays, the keys and key-
values are thus properly referenced by the foreach statement.

Aug 24 '08 #4

P: n/a
Michael Fesser <ne*****@gmx.dewrote in comp.lang.php:
.oO(Patient Guy)
>>I am a newcomer to using PHP but not to programming (C, C++,
Javascript).

I am playing around with classes and wanted to make a function that
has a method simply for producing either plain text or HTML output in
a tabular way a listing of the global variables in the [key] =
keyValue two-column format. That's what the method listGlobals does.

I am stumped by the following error message:

PHP Catchable fatal error: Object of class phpEnv could not be
converted to string in <URL-hereon line 43

There are really few results from Google searches, with results
usually having people ask the same questions with no answers.

Comments on what is causing the error and general tips on class
construction are welcome.

===

The contents of file EnvClass.php are shown below, and line 43 is
commented (don't count lines in text below...code was re-formatted for
posting):

PHP Version 5.2.6

-----------------start of file EnvClass.php
<--------------------------- <?php

class phpEnv {
public $superglobals;
public $superglobalNames;

function phpEnv ()

The constructor should be called __construct(). You're using the old
and deprecated PHP 4 naming.
>>[...]
if ($inHTML == true)

The "== true" is not really necessary if you test an expression which
already is or evaluates to a boolean.
>>[...]
foreach ($this->superglobals[$i] as $key =$value)
if ($inHTML == true)
{
$output .= "<tr><td class=\"key\"";
if (isset($keyStyle) == true)
$output .= " style=\"".$keyStyle."\"";
$output .= ">".$this->superglobalNames[$i].
"[".$key."] =&gt;</td>\n";
$output .= "<td class=\"value\"";
if (isset($valueStyle) == true)
$output .= " style=\"".$valueStyle."\"";
/************** LINE 43 is line immediately below
*********************/
$output .= '>'.$value.'</td></tr>\n';

You're looping through all the superglobals, but the $GLOBALS array
also contains the object of the phpEnv class, which runs this code. If
you try to print it out, you'll get the error message.

Also watch for recursion. The $GLOBALS array contains itself and all
the other superglobals as well!
>>[...]
$output .= $this->superglobalNames[$i].
"[".$key."] == ".$value."\n";

You should use interpolation or sprintf() to keep the code more
readable and to avoid the ugly and sometimes error-prone
concatenation, e.g.

$output .= "{$this->superglobalNames[$i]}[$key] ==$value\n";

or (which is what I usually prefer)

$output .= sprintf("%s[%s] ==%s\n",
$this->superglobalNames[$i],
$key,
$value
);

The sprintf() way also makes it much easier to apply functions like
htmlspecialchars(), which should always be used when printing out
arbitrary values to an HTML page.

Micha
Thanks for the tips. Based on your response, I did the following:

1. Using __construct() instead of deprecated constructor name

2. Using sprintf() instead of the hard-to-read concat style. As an old C
programmer, I should prefer standard C library functions anyway, right?
;-)

3. I got rid of the $GLOBALS array element, and the problem immediately
resolved itself and my code ran without error or exception or syntax
warning. When I put it back in the code, I saw the same run-stopping
warning reported. Almost certainly the code compiler/interpreter
determined that there was some kind of recursion or circular self-
referencing problem.
If you have a page of your own or one that you like on PHP tips and tricks
and a quick reference on proper use of the fundamentals, I would like to
know of it too.
Aug 24 '08 #5

P: n/a
..oO(Patient Guy)
>Thanks for the tips. Based on your response, I did the following:

1. Using __construct() instead of deprecated constructor name

2. Using sprintf() instead of the hard-to-read concat style. As an old C
programmer, I should prefer standard C library functions anyway, right?
;-)
Good. Also have a look at the related function vsprintf(), which might
come in handy as well if you want to pass an array of arguments. And of
course there's also (v)printf() for direct output.
>3. I got rid of the $GLOBALS array element, and the problem immediately
resolved itself and my code ran without error or exception or syntax
warning. When I put it back in the code, I saw the same run-stopping
warning reported. Almost certainly the code compiler/interpreter
determined that there was some kind of recursion or circular self-
referencing problem.
You will get the same error if you try this:

$foo = new phpEnv();
print $foo;

And in fact this is what happens if you include the $GLOBALS array in
your class: At some point when looping through $GLOBALS the interpreter
tries to print out the actual object like above -*boom*

This problem can be solved (for example by adding a __toString() method
to your class), but then you would run into the next one because of the
recursive structure of $GLOBALS, so removing it was the best for now. If
you still like to see the defined global variables, you would have to
add some more checking routines when printing $GLOBALS.
>If you have a page of your own or one that you like on PHP tips and tricks
and a quick reference on proper use of the fundamentals, I would like to
know of it too.
Nope, sorry. I started with PHP 3 and then more or less just did
"learning by doing". But since I came to web development with already
having a programming background (I've learned Pascal and OOP in school),
it wasn't too difficult. The rest and "fine tuning" came with increasing
experience, from various PHP newsgroups and the UCNs (user contributed
notes) in the online manual.

Micha
Aug 24 '08 #6

P: n/a
Patient Guy wrote:
Jerry Stuckle <js*******@attglobal.netwrote in comp.lang.php:
>Patient Guy wrote:
>>I am a newcomer to using PHP but not to programming (C, C++,
Javascript).

I am playing around with classes and wanted to make a function that
has a method simply for producing either plain text or HTML output in
a tabular way a listing of the global variables in the [key] =
keyValue two-column format. That's what the method listGlobals does.

I am stumped by the following error message:

PHP Catchable fatal error: Object of class phpEnv could not be
converted to string in <URL-hereon line 43

There are really few results from Google searches, with results
usually having people ask the same questions with no answers.

Comments on what is causing the error and general tips on class
construction are welcome.

===

The contents of file EnvClass.php are shown below, and line 43 is
commented (don't count lines in text below...code was re-formatted
for posting):

PHP Version 5.2.6

-----------------start of file EnvClass.php
<--------------------------- <?php

class phpEnv {
public $superglobals;
public $superglobalNames;

function phpEnv ()
{
$this->superglobals = array($GLOBALS, $_SERVER, $_GET, $_POST,
$_FILES, $_REQUEST, $_ENV, $_COOKIE);
$this->superglobalNames = array('$GLOBALS', '$_SERVER',
'$_GET',
'$_POST', '$_FILES', '$_REQUEST', '$_ENV', '$_COOKIE');
if (isset($_SESSION) == true)
{
array_push($superglobals, $_SESSION);
array_push($superglobalNames, '$_SESSION');
}
}

function listGlobals ($inHTML=false, $tableID=null,
$keyStyle=null,
$valueStyle=null, $headingStyle=null)
{
$output = "";
if ($inHTML == true)
$output .= "<table id='".$tableID."' class='report'>";
for ($i = 0; $i < count($this->superglobals); $i++)
{
if ($inHTML == true)
{
$output .= "<tr><th colspan=\"2\" class=\"header\"";
if (isset($headingStyle) == true)
$output .= " style=\"".$headingStyle."\"";
$output .=
">".$this->superglobalNames[$i]."</th></tr>\n";
}
else
$output .= $this->superglobalNames[$i];
foreach ($this->superglobals[$i] as $key =$value)
if ($inHTML == true)
{
$output .= "<tr><td class=\"key\"";
if (isset($keyStyle) == true)
$output .= " style=\"".$keyStyle."\"";
$output .= ">".$this->superglobalNames[$i].
"[".$key."] =&gt;</td>\n";
$output .= "<td class=\"value\"";
if (isset($valueStyle) == true)
$output .= " style=\"".$valueStyle."\"";
/************** LINE 43 is line immediately below
*********************/
$output .= '>'.$value.'</td></tr>\n';
}
else
$output .= $this->superglobalNames[$i].
"[".$key."] == ".$value."\n";
}
if ($inHTML == true)
$output .= "</table>";
else
$output .= "\n\n";
return ($output);
}
}

?>
-----------------end of file EnvClass.php
<---------------------------
$superglobals[$i] (which is aliased as $value in your foreach loop) is
an array, not a simple type. You can't string concatenate arrays.

Within the foreach loop, doesn't $this->superglobals[$i] reference a
single PHP-predefined global variable like $_SERVER and the like?

That is, with this code:

foreach ($this->superglobals[$i] as $key =$value)

I am basically saying, as the $i counter is incremented:

foreach ($_SERVER as $key =$value)
foreach ($_ENV as $key =$value)
foreach ($_REQUEST as $key =$value)
and so on

Is this not so?

I am assuming that because they are associative arrays, the keys and key-
values are thus properly referenced by the foreach statement.

Ah, I see now - you're using integer indexes in $this->superglobals[].
Not the way I would have done it, but it works. My mistake.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 24 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.