|
-
Apr 11th, 2006, 09:38 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] what @ and & means in PHP
Hi!
I've searched all over PHP.net but can't figure out what @ means in front of a fonction... I think it is to avoid error message, but I can't be sure.. also I have seen object declaration like $db = &new DB; what does & do??? thank you
-
Apr 11th, 2006, 05:12 PM
#2
Re: what @ and & means in PHP
@ is the error supression operator. It is equivilent to calling the error_reporting(0) before and after the statement. In effect it prevents PHP from spitting out any notices, warnings or fatal error messages. It DOES NOT however, make the error go away, fatal errors still kill the script and warnings or notices which would once be displayed may go un-noticed. You should use the error suppression opertor in conjunction with a check ie:
PHP Code:
if (! ($fhwnd = fopen('blah', 'r')) {
/* file failed to open */
}
The above statement attempts to open a file, if it fails the code inside the If statement is executed. The error supression operator is used to prevent PHP from displaying the warning message generated if it does fail.
& is the variable reference opperator. In PHP a variable identifier: i.e: $foo, is a name used to identify a specific location in memory. This location will contain a value. Sometimes, it may be necessary to have several identifiers which point to the same location - this is especially the case with objects and arrays; most languages infact, including PHP 5+, use references by default for objects.
In contrast to a normal variable identifer, in a reference, the variable name instead referes to a location in memory, which contains another location in memory. When we refer to a referenced variable, PHP looks up that location and fetches the value from there.
PHP Code:
$variable1 = 'hello';
$reference1 = &$variable1;
$variable2 = $variable1;
$reference2 = &$variable1;
echo($variable1); // outputs hello
$reference2 = 'goodbye'; // this changes $reference1 to a variable
echo($variable2); // outputs hello
$variable1 = "goodbye";
echo($reference1); // outputs goodbye
As you can see, a change to the variable causes this change to be reflected by all variables that hold a reference to it, as they all refer to the same location. Unfortunatly PHP does not have a dereference operator, so it is not possible to update the real variable via the reference.
In PHP 4, arrays and objects are always assigned by value, that means that if you create an object and assign it to another variable, all its data is copied into a new location in memory. For objects in particular this behaviour (known as cloning) is rarely required and can lead to problems, for eacmple, cloning an object in PHP 4 causes its contsructor to be called again on the new instance.
It is for this reason that in PHP 4 objects are often created as follow:
PHP Code:
$ref = &new ObjectName;
Now, when the object is assigned to another variable:
It too will hold a reference to the same object.
As of PHP 5, you need to worry about this as all objects are passed by reference as default.
Last edited by visualAd; Apr 11th, 2006 at 05:18 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|