} class rubberDuck extends Duck { 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;
function testDuck(Duck $duck) { $duck->quack(); $duck->fly(); }
public function fly() { if (rand(0, 4) == 0) { $this->duck->fly(); } }
| It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the Closure to execute when that URI is requested. | */ Route::get('/', function () { return Redirect::to('ducks'); }); Route::get('ducks', function () { return View::make('duck-form'); // dd(App::environment()); }); // route to process the ducks form Route::post('ducks', function () { $rules = array('name' => 'required', 'email' => 'required|email|unique:ducks', 'password' => 'required', 'password_confirm' => 'required|same:password'); $validator = Validator::make(Input::all(), $rules); if ($validator->fails()) { $messages = $validator->messages(); return Redirect::to('ducks')->withErrors($validator); } else { $duck = new Duck(); $duck->name = Input::get('name'); $duck->email = Input::get('email'); $duck->password = Hash::make(Input::get('password')); $duck->save(); if ($duck->save()) { return Redirect::to('ducks'); } else { return "There was an error with your entry. Please refresh the page and try again."; } } });
class NoQuack implements QuackBehavior { public function quack() { echo "I don't even quack"; } } class Duck { public $fly_behavior; public $quack_behavior; public function swim() { echo "swimming duck"; } public function display() { echo "be a duck"; } public function performQuack() { $this->quack_behavior->quack(); } public function performFly() { $this->fly_behavior->fly(); } } $duck = new Duck(); $duck->quack_behavior = new MakeNoise(); $duck->performQuack();
public function display() { parent::display(); }
<?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();