Dynadot โ€” .com Registration $8.99

Why this doesn't work? [PHP]

Spacemail by SpaceshipSpacemail by Spaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
PHP:
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 someone explain to me why that gives this error:

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
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
Can someone explain to me why that gives this error:
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

I'm sorry to be Captain Obvious, but "echo" is intended to output strings, not objects :) In your example variable $video_class is an object, so you can't just output it.

Are you familiar with object-oriented programming?
 
0
•••
This will work:

PHP:
class video
{
    protected $text;
    protected $code;

    public function __construct($text,$code="[*]")
    {
        $this->text = $text;
        $this->code = $code;
    }

    public function __toString()
    {
        $html = preg_replace("#\[\*\]#", $this->text, $this->code);

        return $html;
    }
}  

$video_class = new video("HELLO");

echo $video_class;

Reference PHP: Magic Methods - Manual
 
0
•••
Or this should work

PHP:
<?php
class video
{
    public function __construct()
    {
    }

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

        return $html;
    }
}  

$video_class = new video();
echo $video_class->youtube('HELLO', "[*]");
?>
 
0
•••
Use var_dump to display objects
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back