Skip to content

Instantly share code, notes, and snippets.

@davidino
Created July 24, 2011 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidino/1102671 to your computer and use it in GitHub Desktop.
Save davidino/1102671 to your computer and use it in GitHub Desktop.
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