473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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


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
6 3119
..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
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
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
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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dabbler | last post by:
I'm getting the following error when I try and insert a row using FormView, ObjectDataSource and stored procedure. The form has 40+ columns on it and I'm not sure how to diagnose where the problem...
1
by: gp | last post by:
****************************************************************************************************************************** Catchable fatal error: Object of class stdClass could not be...
13
by: seaside | last post by:
I have a method function appendChildNode( AST $aNewChild ) { ... } <<< where AST is a class. If I pass null, PHP renders this message: Catchable fatal error: Argument 1 passed to...
2
by: ghostrider | last post by:
I've been trying to clear these error messages that I keep getting, was wondering if someone could help me out here. 1. Value of type '1-dimensional array of String' cannot be converted to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.