Created
July 24, 2011 14:28
-
-
Save davidino/1102671 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php -S localhost:8124 | |
<?php | |
$a = array( | |
'logger' => function () {echo 'loggin really serious stuff';} | |
); | |
echo $a['logger']() . "\n"; | |
class Car | |
{ | |
protected $manufacturer = NULL; | |
public function getSerialId() | |
{ | |
return rand(0, 999); | |
} | |
public function allowsDriversOver() | |
{ | |
return 18; | |
} | |
public function isA($manufacturer) | |
{ | |
if ($this->manufacturer == $manufacturer) | |
{ | |
return true; | |
} | |
} | |
} | |
class StandardCar extends Car | |
{ | |
} | |
class MiniCar extends Car | |
{ | |
public function allowsDriversOver() | |
{ | |
return 16; | |
} | |
} | |
class BmwCar extends StandardCar | |
{ | |
use BmwManufactured; | |
} | |
class BmwMiniCar extends MiniCar | |
{ | |
use BmwManufactured; | |
} | |
trait BmwManufactured | |
{ | |
protected $manufacturer = 'bmw'; | |
public function getSerialId() | |
{ | |
return parent::getSerialId() . "-bmw"; | |
} | |
public function allowsDriversOver() | |
{ | |
return 21; | |
} | |
} | |
$bmwCar = new BmwCar(); | |
$bmwMiniCar = new BmwMiniCar(); | |
echo 'Macchina BMW: ' . $bmwCar->allowsDriversOver() . ", " . $bmwCar->getSerialId() . "\n"; | |
echo 'Mini BMW: ' . $bmwMiniCar->allowsDriversOver() . ", " . $bmwMiniCar->getSerialId() . "\n"; | |
$car = new car(); | |
echo 'Macchina: ' . $car->allowsDriversOver() . ", " . $car->getSerialId() . "\n"; | |
$miniCar = new Minicar(); | |
echo 'Macchina mini: ' . $miniCar->allowsDriversOver() . ", " . $miniCar->getSerialId() . "\n"; | |
if ($bmwCar->isA('bmw')) | |
{ | |
echo "Ho una BMW!\n"; | |
} | |
class ClosureContainer | |
{ | |
protected $value = 13; | |
public function getValue() | |
{ | |
$a = function () { | |
return $this->value; | |
}; | |
return $a(); | |
} | |
} | |
$cc = new ClosureContainer(); | |
echo $cc->getValue() . "\n"; | |
echo "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment