Skip to content

Instantly share code, notes, and snippets.

View coreymcmahon's full-sized avatar

Corey McMahon coreymcmahon

View GitHub Profile
@coreymcmahon
coreymcmahon / UserModel.php
Last active December 12, 2015 06:38
A model implementation using the repository pattern. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 3
<?php
class UserModel
{
public static function getUser($id)
{
$repository = new UserRepository();
return $repository->find($id);
}
// etc...
}
@coreymcmahon
coreymcmahon / UserModel.php
Created February 7, 2013 11:10
A model implementation using a data access object. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 2
<?php
class UserModel
{
public static function getUser($id)
{
$dao = new DataAccessObject();
return $dao->query('
SELECT * FROM users WHERE id = ' . (int)$id
);
}
@coreymcmahon
coreymcmahon / UserModel.php
Last active December 12, 2015 06:38
A naive model implementation. From the article: PDO for Elegant PHP Database Access, http://www.modernphpbook.com/articles/pdo-for-elegant-php-database-access - Fig 1
<?php
class UserModel
{
public static function connectDb()
{
mysql_connect('localhost', 'username', 'password')
or die('some error');
mysql_select_db('someDb')
or die('could not select db');
@coreymcmahon
coreymcmahon / front.php
Created June 14, 2012 12:33
Using the default implementation of HttpKernel\HttpKernel instead of our own framework code - http://www.symfonycentral.com
<?php
require_once __DIR__.'/../vendor/.composer/autoload.php';
use Symfony\Component\HttpKernel;
// ...
$framework = new Simplex\Framework($dispatcher, $resolver);
$response = $framework->handle($request);
$response->send();
@coreymcmahon
coreymcmahon / container.php
Created June 14, 2012 12:22
Introducing dependency injection into Simplex - http://www.symfonycentral.com
<?php
// example.com/src/container.php
use Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Reference;
$sc = new DependencyInjection\ContainerBuilder();
$sc->register('context', 'Symfony\Component\Routing\RequestContext');
$sc->register('matcher', 'Symfony\Component\Routing\Matcher\UrlMatcher')
@coreymcmahon
coreymcmahon / Framework.php
Created June 14, 2012 12:15
Shifting code to the framework core to manage dependencies - http://www.symfonycentral.com
<?php
namespace Simplex;
use Symfony\Component\Routing;
use Symfony\Component\HttpKernel;
use Symfony\Component\EventDispatcher\EventDispatcher;
class Framework extends HttpKernel\HttpKernel
{
public function __construct($routes)
@coreymcmahon
coreymcmahon / front.php
Created June 14, 2012 12:13
Using a "heavy front controller" to manage dependencies - http://www.symfonycentral.com
<?php
require_once __DIR__.'/../vendor/.composer/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;
use Symfony\Component\HttpKernel;
use Symfony\Component\EventDispatcher\EventDispatcher;
$request = Request::createFromGlobals();
@coreymcmahon
coreymcmahon / front.php
Created June 14, 2012 11:53
Attaching the ExceptionHandler in our HttpKernel based framework - http://www.symfonycentral.com
<?php
require_once __DIR__.'/../vendor/.composer/autoload.php';
use Symfony\Component\HttpKernel;
// ...
$listener = new HttpKernel\EventListener\ExceptionListener('Calendar\\Controller\\ErrorController::exceptionAction');
$dispatcher->addSubscriber($listener);
@coreymcmahon
coreymcmahon / ErrorController.php
Created June 14, 2012 11:52
Example of an Exception Controller that responds to exception events in our framework - http://www.symfonycentral.com
<?php
namespace Calendar\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\FlattenException;
class ErrorController
{
public function exceptionAction(FlattenException $exception)
{
@coreymcmahon
coreymcmahon / Framework.php
Created June 14, 2012 11:43
By extending HttpKernel we can significantly reduce our framework's code - http://www.symfonycentral.com
<?php
namespace Simplex;
use Symfony\Component\HttpKernel\HttpKernel;
class Framework extends HttpKernel
{
}