Three different ways to create a new Smarty object?

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-18-2006
William Krick
 
Posts: n/a
Default 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.

Reply With Quote
  #2 (permalink)  
Old 05-18-2006
rockstar
 
Posts: n/a
Default 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
http://eventuallyanyway.com

Reply With Quote
  #3 (permalink)  
Old 05-18-2006
Chung Leong
 
Posts: n/a
Default Re: Three different ways to create a new Smarty object?


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.

Reply With Quote
  #4 (permalink)  
Old 05-19-2006
William Krick
 
Posts: n/a
Default 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 =&...

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.

Reply With Quote
  #5 (permalink)  
Old 05-19-2006
Chung Leong
 
Posts: n/a
Default Re: Three different ways to create a new Smarty object?


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.

Reply With Quote
  #6 (permalink)  
Old 05-23-2006
kasztelix@gmail.com
 
Posts: n/a
Default Re: Three different ways to create a new Smarty object?


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.

Reply With Quote
  #7 (permalink)  
Old 05-23-2006
Chung Leong
 
Posts: n/a
Default Re: Three different ways to create a new Smarty object?

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.

Reply With Quote
  #8 (permalink)  
Old 05-23-2006
Richard Levasseur
 
Posts: n/a
Default 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 =&

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:13 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0