Why this doesn't work? [PHP]

Namecheap AuctionsNamecheap Auctions
NamecheapNamecheap
SpaceshipSpaceship
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: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back