Skip to content

Instantly share code, notes, and snippets.

@davidino davidino/php5.4.php
Created Jul 24, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.