Blog

  • Symfony 1.1 Validation — Gotchas

    1. sfValidatorAnd requires a patch to work with Propel.

    Get the patch here.

    Read the trac ticket for more details.

    2. A huge difference between 1.0 and 1.1 in form validation deals with error handling.

    1.0 puts the error handling right in the action. In 1.1 you use code in a lib/forms file that looks like this for validation / error handling and corresponds usually to the object being validated. In the example below the code is in lib/forms/AccountForm.class.php

    $this->setValidators(array(
        'username' => new sfValidatorString(array('max_length' => 30))
    ));
    

    3. sfForm::setDefaults doesn’t work quite right with CSRF values.
    you’ll need to patch up code based on this google groups post.

    The documentation right now for 1.1 isn’t updated. E.G. The chapter on forms for the 1.1 documentation as of 12 August 2008 will lead you down the wrong path.

    To keep up to date on what’s going on with symfony, I totally consider doing the following mandatory and not optional:

    • Join the google group, symfony-users
    • Join the irc channel in irc.freenode.net #symfony any time you’ve got a question that needs asking
    • Since the docs might be off, it’s best to just read the source.
  • Matt Knox Rocks The San Francisco Ruby Meetup

    Ya, I’ve read Zed’s rant against Rails. I’ve felt the anger and alienation he’s felt, but in the San Francisco PHP Meetup community. After a core team PHP member used a racial epithet to describe something I wanted to do in PHP, the idea of coding web apps using Ruby on Rails began to have more and more appeal.

    I went to the San Francisco Ruby Meetup today at Marakana, and although I was late, I quickly got Rails running and caught up. By the end of 4 hours of lecture and coding I had a working blog on my laptop, and learned a lot about coding in a totally fun way from Matt Knox. He teaches Ruby on Rails at Sermo in Boston.

    Here are a few notes:

    1. To evaluate a technology Matt asks, “How fast can I set up a ‘Hello World!’ app?” and “How fast can I iterate through the development process?”
    2. Validation should happen in the model. The moment you put it in the controller bad things happen.
    3. To be an Internet Rockstar, you should do WRDD (pronounced word), which is Web Request Driven Development. Put the site up with zero features but a page that gives the users an idea of what the site will be. Code functionality as users ask for it.
    4. “Databases are a giant persistent hash in the sky.” (Matt Knox) What this means is that DBAs are constrained by tradition when it comes to scalable web apps. A database should be used as one big hashtable if it is to be scalable.
    5. Do not work for equity.
    6. Subversion cannot git-stash or git-unstash so use git

    I was so happy just getting a blog working so quickly that I registered HipsterHookups today and want to see if I can get a site working by this weekend.

  • Coder Dudes, For Your Sanity Date Starbucks Barristas!

    Since I’ve moved back from Italy, career has gone from unemployed, stacking candles at Planet Weavers in the Castro never believing there would be another tech boom, re-starting a Web 1.0 company, hacking password protected files from the hard drives of former Enron Employees for an Enron trial, and now having once again a successful tech career.

    Since moving back from Italy 6 years ago I haven’t had a girlfriend. This sort of thing makes me shrug. This blog post is my meditation on being single. I asked around. I’m not the only guy in Silicon Valley who has been single for this long, and it’s not that I haven’t been dating or not putting myself out there.

    As a coder, I make more than the median salary in San Francisco. There are men who are poorer than me and less intelligent than me that have girlfriends. There are some coders who make twice that which along with exercised stock options make them millionaires. Attraction (at least the kind worth having) should have nothing to do with money.

    The kind of woman that has so graciously, kindly and flirtingly said yes to a date with me works in tech, knows a lot of the people that I work with, and is absolutely cute. I think what initially draws such a woman to me is how I handle myself in a lot of situations: calm, cool, collected. She also likes the fact that I have a reputation for discretion.

    Yet, I have had a lot of frustrations in dating women in the tech industry. Something is not working out. I would really like to date someone with a lot of similar work interests because I am passionate about my work. But for one reason or another it just doesn’t work out. This has forced me outside of the box. My ideal date now is with a Starbucks Barrista with a degree in either philosophy or literature – definitely something humanities oriented.

    Starbucks Barrista
    WTF? Why?

    1. Unlike other women higher in the social strata your ambitions are not enough for a woman in the industry. Most women in tech see you as a lottery ticket and expect you to daytrade your way up to 7 figures and beyond. Your Starbucks barrista would be more than happy with your 5 figures and ecstatic about 6.
    2. Your Starbucks barrista with her liberal education gives her – duh? – liberal sensibilities. A lot of women in tech that are recruiters, marketers and designers read Cosmo and its ilk. These are magazines dedicated to the manipulation of straight men by women. This is not to say men aren’t guilty of this, too. They are, and the book, “The Game,” is the choicest example of this. My point is that I haven’t run into folks with degrees in liberal arts that are great manipulators like say the Enron folks or GW & company.
    3. You probably won’t have a chance with a cutie coder. Cutie coders get hit on all the time and are totally smarter than you and smarter than you think. Unless you’ve got the skills of Cal Henderson, forget it. Cutie coders are the buyer; you’re just the seller.
    4. Still not giving up? If you go to any tech party, the guys who really have a shot are the CEOs. It’s as if women divide men into 1st class and 2nd class citizens. Guess which class citizen a CEO is.
    5. The hard earned money of your Starbucks barrista is “worth more” because when she buys you a present it means more to her, it’s a bigger deal.
    6. A Starbucks barrista is trying to figure out her way out of her job. She has her own ambitions, and these are probably more modest than your cutie techie’s ambitions. You might even be able to bankroll one of her projects.
    7. Most Web 2.0 kids met on-line. You can tell your grandkids, how you manned-up and asked her out using this technology called face-to-face IRL. You certainly won’t be penciling yourself in her pbwiki page.
    8. She’ll be grateful that you fixed her computer.
    9. The techie cutie has some idea of your lived experience, including the occasional drudgerie of it. For the Starbucks barrista everything you do is awesome and new.
    10. She’ll round you out. Her liberal education gives her a mind that sees there’s more to life than just money, materialism and its shallow pleasures, that there’s a higher world of ideas and delights.
    11. If things do end, she will leave you better than she found you.
    12. After the break up, you won’t have to read about who she’s hooking up with now in Vallywag.
  • NBCi4 loves Twitter

    I think it’s really cool that NBCi4 got their news anchors onto twitter, especially given this graph detailing the Los Angeles earthquake yesterday:

    Graph of twitter vs. ap

    Maybe since these news anchors are on twitter, you can get your Mom on Twitter… or not.

  • Hopes to Catch Up on His RSS Reading

    I’ve got some reading to do. 2000+ more posts to go, and I’m done.

    picture of rss feeds

  • Fun With GDB, Gnu’s Debugger

    Here’s a pretty compact version of strcmp:

    int bstrcmp(char *s1,char *s2) {
       while(*s1 == *s2++) {
          if(*s1++ == 0){ return 0; }
       }
       return (*(unsigned char *)s1 - *(unsigned char*)--s2);
    }
    

    The source that I used for compiling and calling this version of strcmp is here.

    Compile that code using:
    gcc -o strcmp -g strcmp.c

    Fire up the debugger using:
    gdb strcmp

    You’ll get the gdb prompt:
    (gdb)

    Set a break point at the start:
    b main

    The debugger will echo something like:
    Breakpoint 1 at 0x80483d5: file strcmp.c, line 6.
    (gdb)

    Then run the program:
    run

    The debugger will print out something like:
    Starting program: /home/somedude/bin/strcmp

    Breakpoint 1, main () at strcmp.c:6
    6 {
    (gdb)

    If you type n a few times, you’ll eventually get to some variable assignments.

    (gdb) n
    8	char s1[] = "better";
    (gdb) n
    9	char s2[] = "better than"; /* than this";*/
    (gdb) n
    11	int i_result = 0;
    (gdb) n
    13	i_result = bstrcmp(s1,s2);
    

    If you want to the values of these variables type:
    p i_result

    You get back:
    (gdb) p i_result
    $1 = 0

    To step into a function, type s:

    (gdb) s
    bstrcmp (s1=0xbf86b469 "better", s2=0xbf86b474 "better than") at strcmp.c:26
    26		while(*s1 == *s2++) { 
    (gdb) n
    27			if(*s1++ == 0){ return 0; } 
    (gdb) n
    26		while(*s1 == *s2++) { 
    (gdb) n
    27			if(*s1++ == 0){ return 0; } 
    (gdb) n
    26		while(*s1 == *s2++) { 
    (gdb) 
    

    At this point you can type things like:
    p s1
    p *s1
    p s2
    p *s2

    And you’ll get back the value of the pointers and what’s in memory.

    Next time we’ll go over how to do this with PHP running single threaded on debug mode on Apache.

  • The No-Tech Invite Birthday Celebration

    If you had to plan your birthday celebration and used no technology, who would show up? I embarked on this experiment about a week before my birthday on the 13th. I would only invite people if we were face to face. I wouldn’t use email or snail mail… just simple, literal word of mouth.

    I ran into 5 people before my birthday. 1 I didn’t want to invite because last year he had an unreasonable personal emergency (I have to be in the City for my girlfriend at 2pm) that forced me to cancel my birthday celebration in the forest. And ya, no cell coverage. 2 couldn’t make it because it wasn’t convenient for them. 2 made it because they’re my friends that I’ve been hanging out with at least on a weekly basis.

    On the day of my birthday, I ran into 2 people I knew but hadn’t seen in ages, and they sang, “Happy Birthday” to me from a car while I was walking down the street.

    I had an Evite inspired birthday bash two years ago on the roof top of a building on Carl and Cole. Over 40 people showed up.

    Technology based birthday: 40 people
    Just using physical word of mouth: 2 people

    I don’t want to say one is more real than the other… after all what is reality? Out of the 40 people that were at my technology based birthday, only 1 person showed up to my word of mouth birthday.

  • print_r in PHP-5.2.6 Doesn’t Print the Whole SimpleXML Object

    There’s an interesting SimpleXML related bug in PHP 5.2.6 . print_r is unable to print all the contents of a SimpleXML object. The bug is reported on the php.net site.

    The reproduction of the code is below:

    Reproduce code:
    ---------------
    <?php
    $xmlstr=<<<EOXML
    <?xml version='1.0' standalone='yes'?>l;
    <products>
        <product order_id="0001"></product>
        <product order_id="0002">PHP book</product>
        <product order_id="0003">
            <name>PHP book</name>
        </product>
    </products>
    EOXML;
    
    $xml=new SimpleXMLElement($xmlstr);
    print_r($xml);
    ?>
    
    Expected result:
    ----------------
    @attributes array for second item as well
    
    Actual result:
    --------------
    SimpleXMLElement Object
    (
        [product] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [order_id] => 0001
                            )
    
                    )
    
                [1] => PHP book
                [2] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [order_id] => 0003
                            )
    
                        [name] => PHP book
                    )
    
            )
    
    )
    
    [13 May 1:29pm UTC] felipe@php.net
    
    The attribute can be accessed, however toString really doesn't show it.
    
    print_r($xml->product[1]);
    
    SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [order_id] => 0002
            )
    
        [0] => PHP book
    )

    It’s been pretty interesting going through the code in php-5.2.6/ext/simplexml . A note in the README file provides a clue:

    “Due to engine problems it is currently not possible to access a subelement by index 0: $object->property[0].”

  • #php on irc.he.net harbors a racist coder

    16:43 < barce> @niklas` i’m writing a connect4 game, and noticed that when i traverse
    diagonals acutely or gravely, the only diff between the functions is their
    operators: “>=” or “<=". 16:43 < barce> “++” or “–”
    16:44 < barce> so instead of if ($x < $limit) $x++; i want to define an operator before hand. 16:44 < barce> e.g. $op = “++”; if ($x < $limit) $x{$op}; 16:44 <+impl> so you save one line of code?
    16:45 <@horros> Now why the hell would you want to do that?
    16:45 <@feti> i’m successfully confused
    16:45 < barce> ya. why not?
    16:45 <+impl> because it’s stupid
    16:45 < barce> why is it stupid?
    16:45 <+impl> Because you would have to implement runtime operator resolution so that you
    don’t have to have an else { } block
    16:45 < barce> not being curious is stupid.
    16:46 < barce> right.
    16:46 < barce> that would be pretty hard to do.
    16:46 <@horros> No, being curious is good.
    16:46 < barce> so i’m asking if there’s a language like that.
    16:46 <+impl> I’m sure you could nigger rig it in ruby

    Please, come on down to #php irc.he.net and let this impl dude know what a blight to humanity he is.

  • How to Make Your Own PHP Extension… Quick and Dirty

    This is a cheat sheet on how to extend PHP. I’m learning how to fix bugs in C within the PHP source code right now using what’s google-able, the #php chat room in irc.freenode.net and “Extending and Embedding PHP” by Sara Goleman.

    Preliminaries: make sure you have the right tools for building php from source. Right now these tools are:

    • autoconf: 2.13
    • automake: 1.4+
    • libtool: 1.4.x+ (except 1.4.2)
    • bison: 1.28, 1.35, 1.75, 2.0 or higher
    • flex: 2.5.4 (not higher)
    • re2c: 0.9.11+ (0.12.0+ for HEAD)
    • Get the latest php stable version. as of this blog posting it’s php-5.2.6
    • unpack the file using something like bunzip2 -c < php-5.2.6.tar.bz2 | tar xvf -
    • ./configure –prefix=/usr/local –enable-debug (I’m really old school and like to put my stuff in /usr/local . YMMV.)
    • make ; make install

    If that worked now we’re ready to make a module. Feel free to substitute barce or BARCE in the code below with whatever you want the module name to be.

    • cd ext (if you type ls you’ll see the extensions that come with php.)
    • mkdir barce (I’m a narcissistic, buddhist, nihilist.)
    • create a config.m4 file with this code:
    PHP_ARG_ENABLE(barce,
            [Whether to enable the "barce" extension],
            [  --enable-barce       Enable "barce" extension support])
    
    if test $PHP_BARCE != "no"; then
            PHP_SUBST(BARCE_SHARED_LIBADD)
            PHP_NEW_EXTENSION(barce, barce.c, $ext_shared)
    fi
    
    • then create a php_barce.h file:
    #ifndef PHP_BARCE_H
    /* Prevent double inclusion */
    #define PHP_BARCE_H
    
    /* Define extension properties */
    #define PHP_BARCE_EXTNAME "barce"
    #define PHP_BARCE_EXTVER "1.0"
    
    /* Import configure options
     * when building outside of the 
     * PHP source tree */
    #ifdef HAVE_CONFIG_H
    #include "config.h"
    #endif
    
    /* Include PHP standard Header */
    #include "php.h"
    /*
     * define the entry point symbole
     * Zend will use when loading this module
     */
    extern zend_module_entry barce_module_entry;
    #define phpext_barce_ptr &barce_module_entry
    
    #endif /* PHP_BARCE_H */
    
    
    • create a barce.c file with your functions that you are creating for PHP
    #include "php_barce.h"
    
    PHP_FUNCTION(barce_thinks_youre_cool)
    {
            char *name;
            int name_len;
    
            if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", 
                    &name, &name_len) == FAILURE)
            {
                    RETURN_NULL();
            }
    
            php_printf("Barce thinks you're cool, ");
            PHPWRITE(name, name_len);
            php_printf("!\n");
    }
    
    
    static function_entry php_barce_functions[] = {
            PHP_FE(barce_thinks_youre_cool, NULL)
            { NULL, NULL, NULL }
    };
    
    zend_module_entry barce_module_entry = {
    #if ZEND_MODULE_API_NO >= 20010901
            STANDARD_MODULE_HEADER,
    #endif
            PHP_BARCE_EXTNAME,
            php_barce_functions, /* Functions */
            NULL, /* MINIT */
            NULL, /* MSHUTDOWN */
            NULL, /* RINIT */
            NULL, /* RSHUTDOWN */
            NULL, /* MINFO */
    #if ZEND_MODULE_API_NO >= 20010901
            PHP_BARCE_EXTVER,
    #endif
            STANDARD_MODULE_PROPERTIES
    };
    
    #ifdef COMPILE_DL_BARCE
    ZEND_GET_MODULE(barce)
    #endif
    
    

    Now let’s build this puppy.

    • phpize
    • ./configure –enable-barce
    • make
    • there should now be a modules directory. copy it to /usr/local/lib/php/modules/ .
    • edit php.ini and add two lines:
      • extension_dir = “/usr/local/lib/php/modules”
      • extension = “barce.so”

    Let’s test and see if it worked:

    php -r “barce_thinks_youre_cool(‘Put your name here’);”