note 68158 added to language.references.return
| From: | neozenkai at yahoo dot com | Date: | Sat, 15 Jul 2006 20:38:10 +0000 |
| Subject: | note 68158 added to language.references.return | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-114810@lists.php.net to get a copy of this message | ||
While trying to create a function to return Database connection objects, it took me a while to get
this right:
<?php
class TestClass
{
var $thisVar = 0;
function TestClass($value)
{
$this->thisVar = $value;
}
function &getTestClass($value)
{
static $classes;
if (!isset($classes[$value]))
{
$classes[$value] = new TestClass($value);
}
return $classes[$value];
}
}
echo "<pre>";
echo "Getting class1 with a value of 432\n";
$class1 =& TestClass::getTestClass(432);
echo "Value is: " . $class1->thisVar . "\n";
echo "Getting class2 with a value of 342\n";
$class2 =& TestClass::getTestClass(342);
echo "Value is: " . $class2->thisVar . "\n";
echo "Getting class3 with the same value of 432\n";
$class3 =& TestClass::getTestClass(432);
echo "Value is: " . $class3->thisVar . "\n";
echo "Changing the value of class1 to 3425, which should also change class3\n";
$class1->thisVar = 3425;
echo "Now checking value of class3: " . $class3->thisVar . "\n";
?>
Which outputs:
Getting class1 with a value of 432
Value is: 432
Getting class2 with a value of 342
Value is: 342
Getting class3 with the same value of 432
Value is: 432
Changing the value of class1 to 3425, which should also change class3
Now checking value of class3: 3425
Note that PHP syntax is different from C/C++ in that you must use the & operator in BOTH places,
as stated by the manual. It took me a while to figure this out.
----
Server IP: 64.71.164.2
Probable Submitter: 24.249.192.220
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/language.references.return.php
Edit -- http://master.php.net/note/edit/68158
Del: integrated -- http://master.php.net/note/delete/68158/integrated
Del: useless -- http://master.php.net/note/delete/68158/useless
Del: bad code -- http://master.php.net/note/delete/68158/bad+code
Del: spam -- http://master.php.net/note/delete/68158/spam
Del: non-english -- http://master.php.net/note/delete/68158/non-english
Del: in docs -- http://master.php.net/note/delete/68158/in+docs
Del: other reasons-- http://master.php.net/note/delete/68158
Reject -- http://master.php.net/note/reject/68158
Search -- http://master.php.net/manage/user-notes.php