Skip to content

Instantly share code, notes, and snippets.

View webmozart's full-sized avatar

Bernhard Schussek webmozart

View GitHub Profile
<?php
$form = new Form();
$form->bind($request, $data);
assert true === $form->isBound();
// if request is post, form was submitted
assert true === $form->isXXX();
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(
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
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
The current way, using addOption(), addRequiredOption() and getOption()
+ Concise
+ Error if option is not supported
- Slow, because the addOption() calls are executed for every new instance
- Base class Configurable needed
- Overriding options in parent fields very intransparent
- Options need special treatment ($this->required vs. $this->getOption('required'))
- Problems with accessing required options before parent::__construct()
# Services for a bundle are always loaded
# hello configLoad() will be called automatically (if defined)
# Namespaces can be simplified
doctrine:
dbal:
dbname: xxxxxxxx
user: xxxxxxxx
password: ~
doctrine:
dbal:
dbname: xxxxxxxx
user: xxxxxxxx
password: ~
logging: %kernel.debug%
Using a static OptionSupport class that automatically writes options into properties AND fields extend it.
+ 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
+ Good performance
+ Static options can be hidden from public access
- Private properties wouldn't work, access not allowed from parent classes.
git clone git://github.com/bschussek/symfony-sandbox.git
cd symfony-sandbox
git checkout -b hacking workshop-start
cp app/config/doctrine.yml-dist app/config/doctrine.yml
app/console doctrine:database:create
app/console doctrine:schema:create
app/console doctrine:data:load
<?php
namespace Sensio\HelloBundle;
use Symfony\Component\Form\FormFactory;
use Sensio\HelloBundle\Entity\Post;
use Sensio\HelloBundle\Entity\Comment;
class HelloFormFactory
{