PDA

View Full Version : cannot get this preg replace function to work



ELD
September 1st, 2010, 04:08 PM
class video
{
public function __construct($text)
{
$this->youtube($text, "
");
}

public function youtube($text, $code)
{
$html = preg_replace("#\[\*\]#", $text, $code);

return $html;
}
}

$video_class = new video("HELLO");

echo $video_class;


Can anyone explain what that would error out with:

Catchable fatal error: Object of class video could not be converted to string in /home/prxainf1/public_html/gamingonlinux.info/test.php on line 21

Hellkeepa
September 1st, 2010, 08:20 PM
HELLo!

It means that the object "$video_class" cannot be converted to a string, which is what "echo" expects. Also, your code makes little sense.

Let me explain what it does:
"__contruct ()" is executed upon creating the new object, and recieves the text "HELLO".
This text is then sent to the "youtube ()" method, along with the code " ".
The "youtube ()" method replaces the " " pattern in $code with the content of $text ("HELLO")
It then returns the content to the "__construct ()" method.
"__construct ()" does not do anything else, and quits.
"new" returns the newly created object.
As you see, the modified text does not get saved anywhere, which is what I suspect you want to do. Also, since you don't have any "__tostring ()" method, PHP does not know what you want when trying to use the object as an string.

I'd go for a "printVideo ()" method, however.

Happy syncin'!

ELD
September 1st, 2010, 08:22 PM
Of course the code makes no sense, it was an example so i know how to use stuff correctly on code that does something ;)

Besides i figured it out. I know what does what, all i was wandering was what the error meant.