Skip to content

Instantly share code, notes, and snippets.

@usagi
Last active December 15, 2015 16:28
Show Gist options
  • Select an option

  • Save usagi/5289003 to your computer and use it in GitHub Desktop.

Select an option

Save usagi/5289003 to your computer and use it in GitHub Desktop.
course materials; php lecture / basic database

Whtat's this?

This is the sample codes for students in a php course.

How to try?

  1. clone the repository.
  2. php -S 0.0.0.0:10080 (PHP >=5.4).
  3. access to http://localhost/00_file.php and the other sample.

Contents

  • 00_file.php

    • no use database.
    • learn:
      • how to use a file storage in simple(file_put_contents() and file_get_contents()).
      • what's the serialize() and unserialize().
    • review:
      • setter/getter pattern with __set() and __get() magic methods.
      • basic lambda expression.
      • basic exception.
      • http response header, header() and MIME.
  • 01_sqlite.php

    • use the SQLite3 with PDO.
    • learn:
      • what's PDO.
      • how to use PDO with SQLite3.
    • review:
      • operator + behavior with array parameters.
      • how to use SQLite3 basically.

Tips

  • want you diff?

  • cannot you run 01_sqlite.php in debian/Ubuntu/Mint?

    1. apt-get update
    2. apt-get install php5-cli php5-sqlite sqlite3

License

CC0

Reference

<?php
const NEW_LINE = "\n";
final class game_t
{
const data_file = 'data.txt';
private $conf_;
// getter/setter pattern with php-getter/setter
private function __get($k)
{
switch($k)
{
case 'data':
return $this->conf_;
}
}
private function __set($k, $v)
{
switch($k)
{
case 'data':
$this->conf_ = $v;
break;
}
}
/* // getter/setter pattern with method
private function data($a)
{ return ($a === NULL) ? get_data() : set_data$(a); }
private function get_data()
{ return $conf_; }
private function set_data($a)
{ $conf_ = $a; }
*/
public function conf($a = [])
{
echo
( '----'.NEW_LINE
. 'conf'.NEW_LINE
);
var_dump($a);
$change = function($t, $a)
{
if(!is_array($a))
throw new Exception('invalid argument');
if($a)
{
$t->conf_ = $a;
return true;
}
};
$r = $change($this, $a) ? '' : 'no ';
echo('conf is '.$r.'changed.'.NEW_LINE);
var_dump($this->conf_);
}
public function load()
{
echo
( '----'.NEW_LINE
. 'load'.NEW_LINE
);
$c = file_get_contents(self::data_file);
if($c === false)
{
echo('cannot open the file: '.self::data_file.NEW_LINE);
return;
}
$this->data = unserialize($c);
var_dump($this->conf_);
}
public function save()
{
echo
( '----'.NEW_LINE
. 'save'.NEW_LINE
);
$r = file_put_contents(self::data_file, serialize($this->data));
if($r === false)
throw new Exception
( 'cannot save the configuration file: '
. self::data_file
);
var_dump($this->conf_);
}
}
$main = function()
{
try
{
header('content-type: text/plain; charset=utf-8');
$game = new game_t();
$game->load();
$game->conf($_REQUEST);
$game->save();
}
catch(Exception $e)
{
echo
( NEW_LINE
. '=== EXCEPTION; caught in main ==='.NEW_LINE
. 'FILE : '.$e->getFile().NEW_LINE
. 'LINE : '.$e->getLine().NEW_LINE
. 'CODE : '.$e->getCode().NEW_LINE
. 'MESSAGE: '.$e->getMessage().NEW_LINE
. 'TRACE : '.$e->getTraceAsString().NEW_LINE
);
exit(1);
}
};
$main();
<?php
const NEW_LINE = "\n";
final class CannotCreateTableException
extends Exception
{
protected $message = 'cannot create table.';
}
final class game_t
{
const data_sqlite = 'data.sqlite.db';
private $conf_ = [];
private $db_;
public function __construct()
{
try
{
$this->db_ = new PDO('sqlite:'.self::data_sqlite);
if( ! $this->is_exists_table() )
$this->create_table();
}
catch(CannotCreateTableException $e)
{
echo('CannotCreateTableException: '.$e->getMessage());
throw $e;
}
catch(Exception $e)
{
echo('PDO exception: '.$e->getMessage());
throw $e;
}
}
private function is_exists_table()
{
echo
( '---'.NEW_LINE
. 'is_exists_table'.NEW_LINE
);
$k = 'count(name)';
$q = 'select '.$k
. ' from sqlite_master'
. ' where name = \'data\''
;
$res = $this->db_->query($q);
if($res === false)
throw new Exception('query failed: '.$q);
$ret = (bool)$res->fetch()[0];
var_dump($ret);
return $ret;
}
private function create_table()
{
echo
( '---'.NEW_LINE
. 'create_table'.NEW_LINE
);
$this->db_->query('create table data(key primary key,value)');
if( ! $this->is_exists_table() )
throw new CannotCreateTableException();
echo('table is created.'.NEW_LINE);
}
private function __get($k)
{
switch($k)
{
case 'data':
return $this->conf_;
default:
throw new LogicException();
}
}
private function __set($k, $v)
{
switch($k)
{
case 'data':
$this->conf_ = $v;
break;
default:
throw new LogicException();
}
}
public function conf($a = [])
{
echo
( '----'.NEW_LINE
. 'conf'.NEW_LINE
);
var_dump($a);
$change = function($t, $a)
{
if(!is_array($a))
throw new InvalidArgumentException();
if($a)
{
$t->conf_ = $a + $t->conf_;
return true;
}
};
$r = $change($this, $a) ? '' : 'no ';
echo('conf is '.$r.'changed.'.NEW_LINE);
var_dump($this->conf_);
}
public function load()
{
echo
( '----'.NEW_LINE
. 'load'.NEW_LINE)
;
$q = 'select * from data';
$r = $this->db_->query($q);
if($r === false)
{
echo('query failed: '.$q.NEW_LINE);
return;
}
$r->bindColumn('key' , $k);
$r->bindColumn('value', $v);
$this->conf_ = [];
while($r->fetch(PDO::FETCH_BOUND))
$this->conf_[unserialize($k)] = unserialize($v);
var_dump($this->conf_);
}
public function save()
{
echo
( '----'.NEW_LINE
. 'save'.NEW_LINE
);
foreach($this->data as $k => $v)
{
$q = 'insert or replace into data values('
. '\''.SQLite3::escapeString(serialize($k)).'\''
. ','
. '\''.SQLite3::escapeString(serialize($v)).'\''
. ')'
;
var_dump($q);
$r = $this->db_->query($q);
if($r === false)
echo('query failed: '.$q.NEW_LINE);
}
var_dump($this->conf_);
}
}
$main = function()
{
try
{
header('content-type: text/plain; charset=utf-8');
$game = new game_t();
$game->load();
$game->conf($_REQUEST);
$game->save();
}
catch(Exception $e)
{
echo
( NEW_LINE
. '=== EXCEPTION; caught in main ==='.NEW_LINE
. 'FILE : '.$e->getFile().NEW_LINE
. 'LINE : '.$e->getLine().NEW_LINE
. 'CODE : '.$e->getCode().NEW_LINE
. 'MESSAGE: '.$e->getMessage().NEW_LINE
. 'TRACE : '.$e->getTraceAsString().NEW_LINE
);
exit(1);
}
};
$main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment