Ejemplo n.º 1
0
        $output .= "Currently the world record {$this->common_name} weighed {$this->record_weight}.";
        return $output;
    }
}
// child class so parent class's methods and properties are available.
class Trout extends Fish
{
    public $species;
    //We're calling the parent constructor but also adding a parameter
    function __construct($name, $flavor, $record, $species)
    {
        parent::__construct($name, $flavor, $record);
        $this->species = $species;
    }
    function getInfo()
    {
        $msg = $this->species . " " . $this->common_name . " tastes " . $this->flavor . ". The record " . $this->species . " " . $this->common_name . " weighed " . $this->record_weight;
        return $msg;
    }
}
//Creating a new instance of the object, brook_trout, and passing in the arguments
$brook_trout = new Trout('Trout', 'Delicious', '14 pounds 8 ounces', 'Brook');
$splake_trout = new Trout('Trout', 'Wonderful', '12 pounds 1 ounce', 'Splake');
echo $brook_trout->getInfo();
echo $splake_trout->getInfo();
?>




Ejemplo n.º 2
0
    {
        $this->common_name = $name;
        $this->flavor = $flavor;
        $this->record_weight = $record;
    }
    public function getInfo()
    {
        $output = "The {$this->common_name} is an awesome fish. ";
        $output .= "It is very {$this->flavor} when eaten. ";
        $output .= "Currently the world record {$this->common_name} weighed {$this->record_weight}.";
        return $output;
    }
}
class Trout extends Fish
{
    public $species;
    function __construct($name, $flavor, $record, $species)
    {
        parent::__construct();
        $this->common_name = $name;
        $this->flavor = $flavor;
        $this->record_weight = $record;
        $this->species = $species;
    }
    public function getInfo()
    {
        return $this->common_name . " " . $this->flavor . " " . $this->record_weight . " " . $this->species;
    }
}
$brook_trout = new Trout("Trout", "Delicious", "14 pounds 8 ounces", "Brook");
echo $brook_trout->getInfo();