By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

Three different ways to create a new Smarty object?

P: n/a
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.

May 18 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

May 18 '06 #2

P: n/a

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.

May 18 '06 #3

P: n/a
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.

May 19 '06 #4

P: n/a

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.

May 19 '06 #5

P: n/a

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.

May 23 '06 #6

P: n/a
ka*******@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.

May 23 '06 #7

P: n/a
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 =&

May 23 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.