Skip to content

Instantly share code, notes, and snippets.

View webmozart's full-sized avatar

Bernhard Schussek webmozart

View GitHub Profile
Using a static OptionSupport class that automatically writes options into properties
+ Options are set in the very beginning of the constructor
+ Concise
+ Error if option is not supported
+ Everything is a property, no special treatment of options
~ Medium performance. Reflection is slow, but some things can be cached so it is faster than addOption()
- Options need to be converted to camel case
Using a static Options class with utility functions
+ Type hints (because setters are called)
+ Options are set in the very beginning of the constructor
+ Good performance
+ Everything is a property, no special treatment of options
- Verbose
- No error if unsupported options are given
a) Importing the namespace only
<?php
use Symfony\Component\Form
class ContactForm extends Form\Form
{
protected function configure()
{
$this->add(new Form\TextField('subject', array(
<?php
$form = new Form();
$form->bind($request, $data);
assert true === $form->isBound();
// if request is post, form was submitted
assert true === $form->isXXX();
<?php
class AuthorForm extends Form
{
protected function configure()
{
$this->setDataClass('Application\Entities\Author');
// 1. problem
<?php
class OptionSupport
{
protected static $definitions = array();
public static function getDefinition($class)
{
if (!isset(self::$definitions[$class])) {
self::$definitions[$class] = new OptionDefinition($class);
@webmozart
webmozart / gist:730993
Created December 6, 2010 21:33
ProjectConfiguration adaptation to prevent sessions starting for bots
// avoid session starting for bots
$agents = array(
'googlebot',
'yahoo! slurp',
'baiduspider',
'sosospider',
'bingbot',
'nagios-plugins',
);
@webmozart
webmozart / configurable_vs_optionsupport.php
Created October 25, 2010 15:16
Benchmark to compare runtime Configurable with annotation-based OptionSupport behaviour
<?php
abstract class Configurable
{
private $options = array();
private $knownOptions = array();
private $requiredOptions = array();
<?php
use Symfony\Foundation\UniversalClassLoader;
require_once __DIR__.'/src/Symfony/Foundation/UniversalClassLoader.php';
$loader = new UniversalClassLoader();
$loader->registerNamespace('Symfony', __DIR__.'/src');
$loader->register();
Solution 1:
Constraints can be defined on the top level.
Advantages:
Little code
Disadvantage:
Min does not have a context. If multiple constraints of the same type exist,
they need to be wrapped in another annotation.