



Profil: szoftverfejlesztés
class Category {
var $id;
var $parentId;
var $children;
function Category( $id, $parentId=0 ) {
$this->id = $id;
$this->parentId = $parentId;
}
}
$categories[1] = new Category( 1 );
$categories[2] = new Category( 2, 1 );
$categories[3] = new Category( 3, 1 );
$categories[4] = new Category( 4, 2 );
$categories[5] = new Category( 5, 3 );
foreach ( $categories as $id=>$category ) {
// Az újabb verziók automatikusan referencia
// szerinti átadást használnak:
$categories[$category->parentId]->children[$id] = $categories[$id];
// A régebbiekben ezt nekünk kell kényszerítenünk:
$categories[$category->parentId]->children[$id] = &$categories[$id];
}
class foo {
public $value = 42;
public function &getValue() {
return $this->value;
}
}
$obj = new foo;
// $myValue is a reference to $obj->value, which is 42:
$myValue = &$obj->getValue();
$obj->value = 2;
// prints the new value of $obj->value, i.e. 2.
echo $myValue;
class Singleton {
function getInstance() {
// PHP 4-ben ez a legegyszerűbb módja
// az instance nyilvántartásának
static $instance;
if ( !$instance ) {
$instance = new Singleton();
$instance->test = "A";
}
return $instance;
}
}
$temp = Singleton::getInstance();
$temp->test = "B";
// PHP 5 esetén B lesz, különben A.
print Singleton::getInstance()->test;
class Singleton {
// Jelezzük, hogy a metódus az eredményt
// referencia szerint adja vissza
function &getInstance() {
static $instance;
if ( !$instance ) {
$instance = new Singleton();
$instance->test = "A";
}
return $instance;
}
}
// A visszatérési értéket referencia szerint rendeljük változóhoz
$temp = &Singleton::getInstance();