/    Sign up×
Community /Pin to ProfileBookmark

question about classes

Whats the difference between

[code=php]
$client = new client;
[/code]

and

[code=php]
$client = &new client;
[/code]

?

And also if I have a SESSION variable called client why does the first line overwrite $_SESSION[‘client’] and the second one not.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@jignesh1Nov 30.2006 — bcause u r refernce the $client variable

[URL=http://us2.php.net/manual/en/language.references.whatdo.php]Here is little more abt refrence[/URL]
Copy linkTweet thisAlerts:
@Sid3335Nov 30.2006 — do you have register_globals = On.

if so that could be why the first statement is overwriting the session var.

as to why the second one doesn't i have no idea. i have never instantiated a class by reference.
Copy linkTweet thisAlerts:
@pcthugNov 30.2006 — Since PHP 5, new return reference automatically so using =& in this context is deprecated and produces E_STRICT level message.

[B]Note:[/B] 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.
×

Success!

Help @stephan_gerlach spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...