php - Formatting of Numbers with PDO -
i have moved large php application using mssql_
functions pdo function using mssql driver.
i wrote simple library allows drop in replacement. seems work pretty considering.
however 1 thing bit annoying default format of numbers , particularly numbers defined money in database.
most of smarty template pages previous output number came database someones balance might show as
125.00
however since changing pdo returned as
125.0000
this little annoying , off putting, not end of world.
my question. there workaround / trick / formatting constant or method can use pdo format values differently, or need go manually set format every number in every template throughout app?
so basically, i'd create models represent result-set each table, , use pdo::fetch_class
load data instances of corresponding class. example:
class usertable //extends abstracttable <-- see below { protected $id = null; protected $name = null; protected $email = null; protected $money = null; }
then add getters , setters format/validate data accordingly eg:
public function getmoney() { return sprintf('%.2f', $this->money);//check if not null first, }
next, have abstract class these models, , implement the arrayaccess
interface in there. example, using simple mapping array:
protected $gettermap = [ 'email' => 'getemail', 'id' => 'getid', 'money' => 'getmoney', ];
define tailor-made map in each child, have abstract class use so:
//in abstract class abstractable implements arrayaccess public function offsetget($offset) { if (!isset($this->gettermap[$offset])) { throw new runtimeexception( sprintf('%s not member of %s', $offset, get_class($this)); ); } $getter = $this->gettermap[$offset]; return $this->{$getter}();//use getter, formats data! }
do similar 4 methods in interface, , can use this:
$row = $stmt->fetch(pdo::fetch_class, 'user'); $row['money'];//will call getmoney, , return formatted number
a more complete example:
abstract class abstracttable implements arrayaccess { protected $id = null;//very defined in tables protected $gettermap = [ 'id' => 'getid', ]; protected $settermap = [ 'id' => 'setid', ]; //force child classes define constructor, sets getter/setter maps abstract public function __construct(); public offsetexists($offset) { return isset($this->gettermap[$offset]); //optionally, check if value if not null: isset($arr['keywithnullval']) returns null, too: return isset($this->gettermap[$offset]) && $this->{$offset} !== null; } public offsetget ( mixed $offset ) { if (!isset($this->gettermap[$offset])) { throw new runtimeexception('member not exist'); } $getter = $this->gettermap[$offset]; return $this->{$getter}(); } public offsetset($offset, $value ) { if (!isset($this->settermap[$offset])) { throw new runtimeexception('trying set non-existing member'); } $setter = $this->settermap[$offset]; $this->{$setter}($value); } public offsetunset ($offset) { //same setter, call: //or leave blank $this->{$setter}(null); } } class usertable extends abstracttable { //protected $id = null; in parent protected $name = null; protected $email = null; protected $money = null; public function __construct() { $fields = [ 'name' => 'etname', 'email' => 'etemail', 'money' => 'etmoney', ]; foreach ($fields $name => $method) { $this->gettermap[$name] = 'g' . $method; $this->settermap[$name] = 's' . $method; } } }
obviously, you'll have write getters , setters fields. not worry, though: ide's helpfully generate getters , setters predefined properties @ click of button
Comments
Post a Comment