Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Referencje i zmienne static
cagrET
post
Post #1





Grupa: Zarejestrowani
Postów: 90
Pomógł: 0
Dołączył: 3.04.2003
Skąd: Opole

Ostrzeżenie: (0%)
-----


Otoz jest taki kawalek kodu php:

[php:1:28e0ae4307]
<?php

class Test {

function &factory() {
return new Test;
}

function &singleton() {
static $instance;
if (!isset($instance)) {
$instance =& Test::factory();
}
return $instance;
}

}

$Test1 =& Test::singleton();
$Test1->aaa = 1;

$Test2 =& Test::singleton();

print_r($Test1);
print_r('<br>');
print_r($Test2);

?>
[/php:1:28e0ae4307]

i wyswietla on:

Kod
test Object ( [aaa] => 1 )

test Object ( )


Dlaczego ?


--------------------
code.gosu.pl
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
adwol
post
Post #2





Grupa: Zarejestrowani
Postów: 691
Pomógł: 0
Dołączył: 6.08.2003

Ostrzeżenie: (0%)
-----


Zamień w funkcji Test::singleton:
[php:1:df19184c2f]<?php
$instance =& Test::factory();
?>[/php:1:df19184c2f]
na:
[php:1:df19184c2f]<?php
$instance = Test::factory();
?>[/php:1:df19184c2f]
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 16:30