Re: String passed to object constructor turning into an instance of that object?

From: Date: Thu, 16 Dec 2010 21:29:07 +0000
Subject: Re: String passed to object constructor turning into an instance of that object?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-310086@lists.php.net to get a copy of this message
Tommy Pham wrote:
class SelectBoxOption extends Tag {
     function SelectBoxOption($name, $value, $selected=false) {
       parent::Tag("option", $name);
       $this->addAttribute("value", $value);
       if($selected) {
         $this->addAttribute("selected", '', false);
       }
if ($name == "") { echo "&nbsp;&nbsp;&nbsp;missing name!<br>\n"; } // else { print "&nbsp;&nbsp;&nbsp;name $name<br>\n"; } if ($value == "") { echo "&nbsp;&nbsp;&nbsp;missing value!<br>\n"; }
     }
will parse and execute, but: - the page will contain "missing value!" for each <option> in the <select> this is generating - it will *not* contain "missing name!" - the <option> tags in the final output don't have content or value (they should have both). If I uncomment that else, I get: adding option <name1> with <value1>
     name <value1>
Catchable fatal error: Object of class SelectBoxOption could not be converted to string in <webroot>/includes/classes/core/display/form/input/SelectBoxOption.php on line 12
What's the actual line #12 in the file SelectBoxOption.php? The SelectBoxOption code you presented has 11 lines unless it's a CNP error.
Whups, thought I noted that. I trimmed a couple of blank lines; line 12 in the file is that print in the else. I found trying to print $name triggers the same error anywhere in that function, too; as I noted further down it seems the string that's passed in is getting mutated into an object. (Whose missing toString function is what led me here - but it works fine in PHP 4.3...) -kgd

« previous php.general (#310086) next »