2016-09-25 43 views
1

我有這段代碼。在PHP中使課堂回聲

$cislo1 = new Cislo(5); 
$cislo2 = $cislo1->odecti(2)->pricti(5); 

而這段代碼。

class Cislo 
{ 
    public function __construct($cislo1) 
    { 
    $this->cislo1 = $cislo1; 
    } 
public function pricti($cislo2) 
{ 
    $this->cislo2 = $cislo2; 
$cislo2 = $this->cislo1 + $this->cislo2; 
} 
public function odecti($cislo2) 
{ 
$this->cislo2 = $cislo2; 
$cislo2 = $this->cislo1 - $this->cislo2; 
} 
} 

我如何讓回聲類?我想我需要回聲Cislo。但不知道如何?

+0

回聲名稱:http://php.net/manual/en/function .get-class.php echo class'屬性:echo $ cislo1-> odecti(2) - > pricti(5) – Marcin

+1

如果你想能夠回顯類本身,那麼你需要實現[magic __toString()方法](http://php.net/manual/en/language.oop5.magic.php#object.tostring) –

+0

沒有名爲'cislo1'的實例屬性。另外,'odecti()'方法沒有'return $ this;'所以'$ cislo1-> odecti(2) - > pricti(5);'不起作用。 –

回答

0

所以加

public function __toString() 
{ 
     return $this->cislo1; 

並嘗試這個

echo $cislo1; 

而且有這個 Picture with error

類的