Clarification on chaining

Clarification on chaining

marthaj6289marthaj6289 Posts: 3Questions: 2Answers: 0

I am somewhat confused about the statements in the documentation concerning chaining with PHP and Editor..
I understand that chaining is not available in PHP v5.3 , but chaining is available with the Editor.
Does this mean if you are using PHP v5.3 with the Editor, you can do chaining?

Answers

  • allanallan Posts: 62,225Questions: 1Answers: 10,208 Site admin

    in PHP 5.4+ you can chain off a constructor - so for example you can do:

    new Editor( $db, 'datatables_demo' )
        ->fields(
            new Field( 'first_name' ),
            new Field( 'last_name' )
        )
    

    In 5.3 that would give an error as you can't chain from the constructor in 5.3. Instead, the Editor libraries provide a factory method which you can chain off:

    Editor::inst( $db, 'datatables_demo' )
        ->fields(
            Field::inst( 'first_name' ),
            Field::inst( 'last_name' )
        )
    

    Personally I massively prefer the style of the first example, but PHP5.3 is still used on a lot of servers, so I use that style for the documentation so try and avoid confusion (I guess I haven't somewhere though...)!

    You can chain off methods in 5.3 - it isn't that chaining isn't possible at all - its just that you can't chain from constructors.

    Regards,
    Allan

This discussion has been closed.