Skip to content

Instantly share code, notes, and snippets.

@jamierumbelow
Created May 16, 2011 22:52
Show Gist options
  • Save jamierumbelow/975552 to your computer and use it in GitHub Desktop.
Save jamierumbelow/975552 to your computer and use it in GitHub Desktop.
A potential, CoffeeScript-esque nicer PHP
// ----------------------
// NO <?php TAG!
// ----------------------
// ----------------------
// SIGNIFICANT WHITESPACE ELIMINATES THE NEED
// FOR CURLY BRACES (YAY)
// ----------------------
class Sessions extends MY_Controller
// ----------------------
// WHY DO VARIABLES NEED TO BEGIN WITH A DOLLAR?
// SIMILARILY, WHY DO METHOD CALLS NEED PARENTHESIES?
// ----------------------
models = array 'user'
// ----------------------
// METHODS DON'T NEED THE 'function' KEYWORD, IT IS DROPPED
// AND A DASHROCKET IS USED INSTEAD. LIKE SO:
//
// function blah($name, $email)
//
// BECOMES...
//
// blah (name, email) ->
// ----------------------
__construct ->
// ----------------------
// NOT SURE ABOUT THIS... 'parent::' becoming '^'
// ----------------------
^__construct()
// ----------------------
// $this IS REPLACED WITH @, AND static:: IS REPLACED
// WITH @@. THE -> OPERATOR IS REPLACED WITH .
// ----------------------
@load.model 'session_model'
// GET /sessions/new
// ----------------------
// METHODS THAT ARE ONE LINE DON'T NEED THE INDENTATION
// ----------------------
get_new -> @title = 'Login'
// POST /sessions
post_create ->
// ----------------------
// METHODS THAT RETURN AN ASSOC ARRAY AND ARE THEN
// ASSIGNED TO MULTIPLE VARIABLES CAN BE ASSIGNED WITH MASS-ASSIGNMENT,
// ELIMINATING THE NEED FOR A $session VARIABLE.
//
// email, password = @input.post 'session'
//
// BASICALLY MEANS
//
// $session = $this->input->post('session');
// $email = $session['email'];
// $password = $session['password'];
//
// ALSO WORKS WITH NUMBERED ARRAYS
//
// one, two = some_array_method()
//
// MEANS
//
// $array = some_array_method();
// $one = $array[0];
// $two = $array[1];
// ----------------------
email, password = @input.post 'session'
// Try to log us in
// ----------------------
// NO NEED FOR if (!...), JUST USE unless. ALSO, ALL SUPERFLUOUS
// PUNCTUATION (brackets, braces etc) ARE REMOVED.
// ----------------------
unless @session_model.login email, password
@session.set_flashdata 'error', @session_model.login_error
redirect 'sessions/new'
// Cool! Redirect to the products page and kick some arse
@session.set_flashdata 'message', 'Successfully logged in'
redirect 'products'
// POST /sessions/delete
post_delete ->
@session.unset_userdata 'user_id'
// Redirect
@session.set_flashdata 'message', 'Successfully logged out'
redirect 'sessions/new'
// GET /sessions/edit
get_edit -> @title = 'Update Account'
<?php
class Sessions extends MY_Controller {
public $models = array('user');
public function __construct() {
parent::__construct();
$this->load->model('session_model');
}
// GET /sessions/new
public function get_new() {
$this->title = 'Login';
}
// POST /sessions
public function post_create() {
// get the POST vars
$session = $this->input->post('session');
$email = $session['email'];
$password = $session['password'];
// Try to log us in
if (!$this->session_model->login($email, $password)) {
$this->session->set_flashdata('error', $this->session_model->login_error);
redirect('sessions/new');
}
// Cool! Redirect to the products page and kick some arse
$this->session->set_flashdata('message', 'Successfully logged in');
redirect('products');
}
// POST /sessions/delete
public function post_delete() {
// Destroy the user session
$this->session->unset_userdata('user_id');
// Redirect
$this->session->set_flashdata('message', 'Successfully logged out');
redirect('sessions/new');
}
// GET /sessions/edit
public function get_edit() {
$this->title = 'Update Account';
}
}
@yorickpeterse
Copy link

Use Python and save yourself a lot of trouble.

@utkarshkukreti
Copy link

Is this just a concept, or have you written a parser for this?

@harlantwood
Copy link

I'm looking for solutions like this too. See also:
https://github.com/unu/ladyphp
https://github.com/c9s/gutscript#synopsis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment