Skip to content

Instantly share code, notes, and snippets.

View mpmont's full-sized avatar

Marco Monteiro mpmont

View GitHub Profile
@mpmont
mpmont / news.php
Created November 26, 2012 12:44
hmvc
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends CI_Controller {
public function index()
{
$this->load->model('news_model', 'article');
$data['news] = $this->article->get_all();
$this->load->view('news_list', $data);
}
$('#subscribe_newsletter').click(function(){
event.preventDefault();
var form_data = {
name: $('#name').val(),
email: $('#email').val()
};
$.ajax({
url: $('#newsletter_url').val(),
type: 'POST',
data: form_data,
@mpmont
mpmont / gist:3846327
Created October 6, 2012 22:07
__clone() method
<?php
class SubObject
{
static $instances = 0;
public $instance;
public function __construct() {
$this->instance = ++self::$instances;
}
@mpmont
mpmont / a.php
Created October 6, 2012 22:04
__set_state() method
class A
{
public $var1;
public $var2;
public static function __set_state($an_array) // As of PHP 5.1.0
{
$obj = new A;
$obj->var1 = $an_array['var1'];
$obj->var2 = $an_array['var2'];
@mpmont
mpmont / Callable.php
Created October 6, 2012 22:00
using _invoke()
<?php
class CallableClass
{
public function __invoke($x)
{
var_dump($x);
}
}
$obj = new CallableClass;
$obj(5);
@mpmont
mpmont / Test.php
Created October 6, 2012 21:55
__toString() example
<?php
class TestClass
{
public $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
@mpmont
mpmont / connection.php
Created October 6, 2012 21:51
Sleep and wakeup
<?php
class Connection
{
protected $link;
private $server, $username, $password, $db;
public function __construct($server, $username, $password, $db)
{
$this->server = $server;
$this->username = $username;
@mpmont
mpmont / PropertyTest.php
Created October 6, 2012 21:44
__get(), __set(), __isset() and __unset() methods
<?php
class PropertyTest
{
/** Location for overloaded data. */
private $data = array();
/** Overloading not used on declared properties. */
public $declared = 1;
/** Overloading only used on this when accessed outside the class. */
@mpmont
mpmont / MyDestructableClass.php
Created October 6, 2012 21:32
construct destruct
<?php
class MyDestructableClass {
function __construct() {
print "In constructor\n";
$this->name = "MyDestructableClass";
}
function __destruct() {
print "Destroying " . $this->name . "\n";
}
@mpmont
mpmont / Call_callStatic.php
Created October 6, 2012 21:21
magic methods php
<?php
class MethodTest
{
public function __call($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}