Tuesday 11 June 2013

Coffeescript for PHP developers


Well I decided to teach my self Coffeescript and was hoping to find a little tutorial for people coming from a PHP background, but was surprised when the internet came up short.
Well, see a need.. fill a need!

So heres a few examples from Charles Wood's CoffeeScript Basics and I have added what would be the equivalent PHP to hopefully give people a reference point.

CoffeeScript: Comment
#this is a comment

PHP
//this is a comment

CoffeeScript: Function
intro = (name)->
    "Hi " + name

Console.log intro("Brian")
The last line in a coffeescript function is returned

PHP
function intro($name){
return 'Hi'.$name;
}

echo intro('Brian');



CoffeeScript: Default Parameter
intro = (name, gender = "friend")->
    "Hi " + name + " my " + gender 

Console.log intro("Brian")

PHP
function intro($name, $gender  = 'friend'){
return 'Hi'.$name.' my '.$gender ;}

echo intro('Brian');


CoffeeScript: Modify A Argument's Value
intro = (name, gender = "friend")->
    gender = gender +"!";
    "Hi " + name + " my " + gender 

Console.log intro("Brian")

PHP
function intro($name, $gender  = 'friend'){
$gender  .= '!';
return 'Hi'.$name.' my '.$gender ;
}

echo intro('Brian');


CoffeeScript: String Interpolation
myScore = 6

console.log "my score was #{myScore}"


PHP
$myScore = 6;

echo "my score was $myScore"



CoffeeScript: Chained Comparisons
myScore = 6

pass = 10 > myScore > 5


PHP
$myScore = 6;

$pass = 10 > $myScore ? $myScore > 5 : false;



CoffeeScript: Hash Map
items = {a:'z', b:'y', c:'x'}

PHP
$items = ['a'=>'z', 'b'=>'y', 'c'=>'x'];


CoffeeScript: Array
items = [
    1,0,0
    0,1,0
    0,0,1
]
CoffeeScript will automatically add a comma at the end of each line in an array is not present

PHP
$items = [   
   1,0,0,
   0,1,0,
   0,0,1
];


CoffeeScript: If
x = true
y = false

if x and y
    console.log true
else
    console.log false

PHP
$x = true
$y = false

if( $x and $y)
    echo 'true';
else
    echo 'false';
Here the example of using endif
if( $x and $y):
    echo 'true';
else:
    echo 'false';
endif


CoffeeScript: Ternary Operator
if x or y then console.log true else console.log false
coffee supports and, or
PHP

echo $x or $y ? 'true' : 'false'


CoffeeScript: Not With Assignment
x = null
x or= true
      

PHP
$x = null;
$x = !$x ? true : $x;
      
for this, the ternary operator must have an else. So i'm using it's own value to simulat no effect.


CoffeeScript: Splats(Function Overloading)
party = (first, second, rest...) ->
      console.log first
      console.log second
      console.log rest
      

PHP
function party(first, second){
      echo $first;
      echo $second;
      $rest = func_get_args();
      array_shift($rest);//remove first
      array_shift($rest);//remove second
      echo implode(',',$rest);
}
      
You don't need implode, I'm just using it to print the exter value from this array.


CoffeeScript: For In/Each
hello = {en: "hi", fr:"Boj", it,"bon"}
speak = (lang) ->
      console.log hello[lang]
speak language for language in ["en","fr","it"]
      

PHP
$hello = ['en'=>'hi', 'fr'=>'Boj', 'it'=>'bon'];
function speak($lang){
      global $hello;
      echo $hello[$lang];
}
      foreach(['en','fr','it'] as $language) speak($language);
      


CoffeeScript: Sequence
countdown = (num for num in [10..1])
countdown = (num*2 for num in [10..1])
      

PHP
$countdown = range(10,1)
$countdown = range(10, 1, 2)
      


CoffeeScript: Parsing An Array
ages = {chuck:31, steven:29, julie:27}
tell = for person, age of ages
person + " is "+age+" years old"
      

PHP
$ages = ['chuck' => 31, 'steven' => 29, 'julie' => 27];
      foreach($ages as $person=> $age) 
      $tell[] = $person.' is '.$age.' years old';
      


CoffeeScript: Class
class Vehicle
      move: (miles) ->
            console.log miles+"miles"
car = new Vehicle
car.move(1000)
      

PHP
class Vehicle{
      public function move($_miles){
            echo $_miles.' miles';
      }
}
      $car = new Vehicle();
      $car->move(1000);
      


CoffeeScript: Attributes
class Vehicle
      setMiles: (@miles)
      getMiles: -> @miles 
car = new Vehicle
car.setMiles(1000)
console.log car.getMiles
      

PHP
class Vehicle{
      public $miles
      public function setMiles($_miles){
            $this->miles = $_miles;
      }
      public function getMiles(){
            return $this->miles;
      }
}
$car = new Vehicle();
$car->setMiles(1000);
echo $car->getMiles();
      


CoffeeScript: Inheritance
class Vehicle
      constructor: (@name) ->
      move: (miles) ->
            console.log @name + " drove "+miles+"miles"

class VWBug extends Vehicle
      move: ->
            console.log "fast"
            super 100

class Truck extends Vehicle
      move: ->
            console.log "slow"
            super 20

mater = new Truck "Mater"
her = new VWBug "Herbie"
mater.move()
her.move()
      

PHP
class Vehicle{
      private $name;
      public function __constructor($_name){
      $this->name = $_name;
      }
      public function move($_miles){
      echo $this->name.' dove '.$_miles.' miles';
      }
      }
      class VWBug extends  Vehicle {
      public function move($_miles = 100){
      echo 'fast';
      parent::move($_miles);
      }
      }
      class Truck extends  Vehicle{
      public function move($_miles = 50){
      echo 'slow';
      parent::move($_miles);
      }
      }
      $mater = new Truck("Mater");
      $her = new VWBug("Herbie");
      $mater->move();
      $her->move();
      

No comments:

Post a Comment