/shy-lib

My collection of little helpers for PHP.

Primary LanguagePHPGNU General Public License v3.0GPL-3.0

Shy Library

This library is my effort to bundle all helper methods in one place for easy integration into my PHP projects. They are usually designed for edge purposes, but will probably fit other uses. I called it Shy because it should behave shily. It’s small. It’s fast.

I publish the components as they mature.

Database

The base class Database wraps around a mysqli object and only exports useful functionality. You can use it like this:

use \Shy\Database\Database;
// …
$db = new Database(array(
    'host' => 'my.db-server.tld',
    'user' => 'me',
    'password' => 'topsecret',
    'database' => 'my_database',
));

You can pass the credentials in the first parameter as an array; otherwise they will be read from your configuration.

What about queries?

$num_clients = $db->query('SELECT COUNT(*) FROM clients')->fetch_value();

That was easy. If you don’t need the result, you can execute queries directly:

$success = $db->execute('DROP TABLE clients');

Table Abstraction

It also features table abstraction that is directly read from the database. Read the product with primary key 12 and its category from the database:

$product = $db->table('products')->by_id(12);
$category = $db->table('categories')->referenced_by($product, 'products')->fetch_row();

Session

The session class is documented inside its file. Basic usage is:

class MySession extends \Shy\Session\AbstractSession
{
    protected function __construct()
    {
        // Set up AbstractSession
    }
}
$session = MySession::get_instance();

The base class provides everything that you should want, such as secure cookie settings, automatic pickup, IP locking and deletion of session cookies.

Forms

Use indexes to find input values inside $_GET or $_POST:

$index = new \Shy\Forms\NestedIndex(array('person', 'name', 'first'));
$index->lookup($_POST); // returns $_POST['person']['name']['first'];

Miscellaneous

Some utilities reside in util.inc. They are not sorted and mostly consist of little macros.