Skip to content

Instantly share code, notes, and snippets.

@davidino
Created July 24, 2011 14:28

Revisions

  1. davidino revised this gist Jul 24, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions php5.4.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    php -S localhost:8124

    <?php

    $a = array(
  2. davidino created this gist Jul 24, 2011.
    104 changes: 104 additions & 0 deletions php5.4.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    <?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";