Three different ways to create a new Smarty object?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William  Krick

    Three different ways to create a new Smarty object?

    I'm fairly new to PHP and Smarty templates and I don't understand...

    $smarty = new Smarty;

    ....vs...

    $smarty = new Smarty();

    ....vs...

    $smarty =& new Smarty;


    I've seen all three used and I'm not sure why/when to use one version
    over another.

  • rockstar

    #2
    Re: Three different ways to create a new Smarty object?

    William,

    The first two are pretty much the same. You don't really NEED to put
    the () at the end of the Smarty object, but it's one of those "nice
    things to do" in your code. It just helps people see creation of an
    instance of the smarty object. I can follow code better that way.
    Smarty itself doesn't require you to pass anything into it on
    instantiation, but some objects do, so it's good to get into the habit.
    The last one, I BELIEVE is creating a reference. I don't completely
    understand php references (as my "mentor" of sorts doesn't really
    either), but I've never had a need to use =&, and it seems to me to be
    a hack rather than serve a good purpose. Stick with the middle one,
    and it'll make everything much cleaner.

    Paul


    Comment

    • Chung Leong

      #3
      Re: Three different ways to create a new Smarty object?


      rockstar wrote:[color=blue]
      > The last one, I BELIEVE is creating a reference. I don't completely
      > understand php references (as my "mentor" of sorts doesn't really
      > either), but I've never had a need to use =&, and it seems to me to be
      > a hack rather than serve a good purpose. Stick with the middle one,
      > and it'll make everything much cleaner.
      >
      > Paul
      > http://eventuallyanyway.com[/color]

      In this instance the use of =& is probably useless. There are specific
      situations though where you must use it. Say you have the following:

      class Node {
      var $parent;
      var $child;

      function Node(&$parent) {
      $parent->child =& $this;
      }
      }

      $node = new Node($parent);

      Because the = operator is assignment by-value, a copy of the new object
      is assigned to $node. Thus $node and $parent->child end up pointing to
      different objects, unlikely to be the desired result.

      Comment

      • William  Krick

        #4
        Re: Three different ways to create a new Smarty object?

        I'm using Smarty templates with PEAR HTML_QuickForm on a server running
        PHP 4.4.1.

        This Smarty tutorial uses =&...



        ....most other examples I've seen use = and I just want to make sure
        that I'm doing the right thing.

        Comment

        • Chung Leong

          #5
          Re: Three different ways to create a new Smarty object?


          William Krick wrote:[color=blue]
          > I'm using Smarty templates with PEAR HTML_QuickForm on a server running
          > PHP 4.4.1.
          >
          > This Smarty tutorial uses =&...
          >
          > http://www.midnighthax.com/smarty-guide.php
          >
          > ...most other examples I've seen use = and I just want to make sure
          > that I'm doing the right thing.[/color]

          It's redundant. = and =& do the same thing. The author does it either
          because he doesn't understand PHP's copy-on-write mechanism or he
          trying to be consistent between PHP 4 and PHP 5, where objects are
          always assigned by reference.

          Comment

          • kasztelix@gmail.com

            #6
            Re: Three different ways to create a new Smarty object?


            Chung Leong napisal(a):[color=blue]
            > It's redundant. = and =& do the same thing. The author does it either
            > because he doesn't understand PHP's copy-on-write mechanism or he
            > trying to be consistent between PHP 4 and PHP 5, where objects are
            > always assigned by reference.[/color]

            I belive it is not redundant. Look here:


            Not using the & operator causes a copy of the object to be made. If you
            use $this in the class it will operate on the current instance of the
            class. The assignment without & will copy the instance (i.e. the
            object) and $this will operate on the copy, which is not always what is
            desired. Usually you want to have a single instance to work with, due
            to performance and memory consumption issues.

            While you can use the @ operator to mute any errors in the constructor
            when using it as @new, this does not work when using the &new
            statement. This is a limitation of the Zend Engine and will therefore
            result in a parser error.

            Comment

            • Chung Leong

              #7
              Re: Three different ways to create a new Smarty object?

              kasztelix@gmail .com wrote:[color=blue]
              > I belive it is not redundant. Look here:
              > http://uk.php.net/manual/en/language...ces.whatdo.php
              >
              > Not using the & operator causes a copy of the object to be made. If you
              > use $this in the class it will operate on the current instance of the
              > class. The assignment without & will copy the instance (i.e. the
              > object) and $this will operate on the copy, which is not always what is
              > desired. Usually you want to have a single instance to work with, due
              > to performance and memory consumption issues.[/color]

              PHP uses copy-on-write. Variable separation would only occur if there's
              a reference to the object returned.

              Comment

              • Richard Levasseur

                #8
                Re: Three different ways to create a new Smarty object?

                If you are using PHP4, use =&, it will assign by reference just like
                PHP5 will do. If the official tutorial/guide/manual is using =&, then
                you should too if you're using PHP4; there is probably a reason they
                are using it.

                =& is redundant in PHP5 with respect to objects, so you can happily
                ignore using =&

                Comment

                Working...