This is a discussion on Three different ways to create a new Smarty object? within the PHP Language forums, part of the PHP Programming Forums category; I'm fairly new to PHP and Smarty templates and I don't understand... $smarty = new Smarty; ....vs... $smarty = new ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 http://eventuallyanyway.com |
|
|||
|
rockstar wrote: > 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 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. |
|
|||
|
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. |
|
|||
|
William Krick wrote: > 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. 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. |
|
|||
|
Chung Leong napisal(a): > 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. 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. 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. |
|
|||
|
kasztelix@gmail.com wrote:
> 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. PHP uses copy-on-write. Variable separation would only occur if there's a reference to the object returned. |
|
|||
|
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 =& |
![]() |
| Thread Tools | |
| Display Modes | |
|
|