Created
February 7, 2012 19:43
-
-
Save rATRIJS/1761471 to your computer and use it in GitHub Desktop.
Random OOP example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DB { | |
public static function insert($table, $properties) { | |
// izveido SQL un izpildi | |
} | |
} | |
class Model { | |
public function __construct($properties) { | |
foreach($properties as $property_name => $property_value) | |
if(property_exists($this, $property_name)) $this->$property_name = $property_value; | |
} | |
public function create() { | |
$properties = array(); | |
foreach($this as $name => $value) | |
$properties[$name] = $value; | |
$this->id = DB::insert(static::TABLE_NAME, $properties); | |
} | |
} | |
class User extends Model { | |
const TABLE_NAME = 'users'; | |
public $id; | |
public $name; | |
public $email; | |
} | |
$user = new User(array( | |
'name' => 'Name', | |
'email' => 'example@example.com' | |
)); | |
$user->create(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment