Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Why this doesn't work? [PHP]

SpaceshipSpaceship
SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
Close
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
Close
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
Close
Use var_dump to display objects
 
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Filters for activity feeds
Your news feed, Latest activity, and the activity tab on profiles now have filter buttons: All, Posts, Quick replies, and Other. Your...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back