Previously, I created a php database adapter that calls mysql stored procedures as if they were local php functions. Now I want to refactor each of the features of the DB adapter into a decorator design pattern. The core database adapter encapsulates the connection config and provides lazy-loading connections for both write-master and read-slave. I […]
php
php DB adapter, revisited
I would like to call mysql stored procedures as if they were local php functions. For example, let’s use the following mysql stored procedure, DELIMITER // CREATE PROCEDURE `usersByPage`(page_num INT, per_page INT) BEGIN SET @lim_start = (page_num – 1) * per_page; SET @lim_end = per_page; PREPARE stmt FROM ‘SELECT * FROM foobar_users ORDER BY user_id […]
php echo and print
Is echo really, even negligibly, faster than print in php? Conventional wisdom of the Internet says “yes”, because print performs one extra op (it returns a value). I’ve seen performance charts with 20% or more difference in performance. I tested this and for the life of me could not find any significant difference. This is […]
get_magic_quotes_gpc(), or not
I would like to sanitize user-supplied HTTP request variables before sending them to a database. There is a deprecated PHP setting called magic_quotes_gpc that automatically escapes GET, POST, and COOKIE (GPC) operators. Please understand that magic_quotes_gpc is highly discouraged. <?php // in a world with magic_quotes_gpc $lastname = get_magic_quotes_gpc() ? stripslashes($_GET[‘lastname’]) : $_GET[‘lastname’]; // if […]
last_insert_id()
I would like to retrieve the id of a row I just inserted. In Oracle this problem is handled with the use of sequences, SQL> SELECT uid_seq.NEXTVAL INTO user_id FROM DUAL; SQL> INSERT INTO users (id, username) VALUES (user_id, ‘joebob’); 1 row created. SQL> — do something else with user_id Basically, a sequence can provide […]
php DB adapter, with magic
I would like to encapsulate the handling of database connections, and maintain a loose coupling between database connections and application and data-access code. I’ll not focus on connection pooling since that can be handled at the driver level independent of the application and adapter code. I would like to leverage the PDO interface as well […]
php Namespaces and Class loading
I want to autoload php classes and functions. Ideally, I want a lazy loader (include class files only when they are needed, never including more files than necessary). I’d also like namespace protection (if running PHP 5.3 or later). And of course I want to do this easily without having to keep track of hierarchical […]