TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
Forums are in read-only mode while we transition to new software.
views: difference between assign, bind, and set
  • What are the differences when assigning a value to a view, binding a value to a view, and setting a value to a view?
  • Ok, here goes:

    // The following two examples are exactly the same
    $view->foo = 'bar';
    $view->set('foo', 'bar');
    

    The only difference here is that using set() allows you to chain:

    $view->set('foo', 'bar')->set('faa', 'baa');
    

    But bind() assigns a variable by reference:

    $view->bind('foo', $bar);
    $bar = 'bar';
    $bar = 'x';
    

    When you use bind, you can change the variable as much as you want and the view will reflect the last changed value.

  • I believe bind() also permits you to never set the value. I'm almost sure if I remember correctly, but would have to test it.

    You can then check in your view if the value has been set or not.


    $view->bind('foo', $bar);
    // we don't set bar afterwards.


    Am I right?
  • Maybe I'm stating the obvious here but anyway...

    One might believe that using bind() instead of set() saves memory because variables don't have to by *copied* to the view. But since PHP uses "copy on write" ( http://www.google.com/#hl=en&source=hp&q=php+copy+on+write ), this is not the case. Bind and set should use just the same amout of memory (that is, none, unless you change view parameters in your views).

    correct me if I'm wrong
  • Posted By: xenakisI believe bind() also permits you to never set the value. I'm almost sure if I remember correctly, but would have to test it.


    you're correct, variables passed by reference will be created if they don't exist... very useful

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion