Exemplo n.º 1
0
{
    public function __construct()
    {
        $this->sound = "Squeek Squeek";
    }
}
class decoyDuck extends Duck
{
    public function __construct()
    {
        $this->sound = "";
    }
}
$darkwing = new Duck("Drake Mallard");
$darkwing->speak("I am the terror that flaps in the night.");
$bill = new rubberDuck("bad Brig");
$bill->speak("You're the one");
$decoy = new decoyDuck();
$decoy->speak("3...2...1...");
try {
    $decoy->speak("");
} catch (Exception $ex) {
    echo "failed";
}
echo "<br>";
// function add($x,$y){
// 	if(is_numeric($x)){
// 		if(is_numeric($y)){
// 			return $x + $y;
// 		} else {
// 			throw new Exception("Invalid Parameter: Must be number");
Exemplo n.º 2
0
<?php

class Duck
{
    private $name;
    protected $sound;
    public function __construct($name)
    {
        echo "I am alive";
        $this->name = $name;
        $this->sound = "UNKNOWN";
    }
    public function speak()
    {
        echo "I can speak " . $this->sound;
    }
}
class rubberDuck extends Duck
{
    public function __construct()
    {
        $this->sound = "Squeek Squeek";
    }
}
$darkwing = new Duck("Drake Mallard");
$darkwing->speak();
$bill = new rubberDuck("bad Brig");
$bill->speak();