473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

May 18 '06 #1
7 3003
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

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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brian | last post by:
Hello, I am trying to work out how to have smarty use a string's contents as the display-> template.. ie: $smarty->display($buffer); obviously $buffer isn't a filename, but a smarty template...
2
by: Daan | last post by:
Hello all, I'm having a little problem with Smarty. Getting it to work in a simple case went fine, but now I have the following situation: / /smarty /templates template.tpl /templates_c
1
by: Richard | last post by:
I need help. I have a smarty based program that I am modifying. I have a smarty template file that consists of smarty and HTML. I need to integrate some PHP database calls into it. My problem...
2
by: M | last post by:
Hi Folks, I am trying to install Smarty using PHP 5 on a Windows XP PC using IIS v6 The following PHP File: <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head>
0
by: Matt | last post by:
geez, i've done this a million times, you'd think i could get it right. uploading to production server for a new site, and part of the head.inc that starts everything up looks like this: echo...
0
by: Conrad | last post by:
Hi, I'm very new to Smarty Template Engine and I'm using PHP 5.0.4 on the Windows XP Professional platform and I have the following question: When I change the 7th line of the script to use a...
0
by: grezlik | last post by:
Hi! I have major problems with cakephp & smarty, (cake ver is 1.2 and smarty 2.6.18 ) there is a code (from cake/libs/view/view.php class SmartyView { .. .. ..
2
dlite922
by: dlite922 | last post by:
Hey guys, this is so weird, i don't know what else to do. Hopefully one of you is a Smarty guru as this is difficult to search (google and smarty forum) and if you aren't i'll post answer here, i'm...
1
by: ajaymohank | last post by:
hello friends.......... i am new to smarty. can anyone please tell me how to get the value from a dynamically generated checkbox using smarty. this is the code i used... here i am generating 10...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.