Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 80
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Przyznam, że jestem pod sporym wrażeniem. Od wielu lat zajmuje się grafiką przeznaczoną do druku ze szczególnym uwzględnieniem opakowań. Z radością stwierdzam, iż twórca serwisu jest moim ulubionym typem potencjalnego współpracownika (choć branża troszeczkę inna) tzn. pada pytanie i błyskawicznie pada konkretna odpowiedź bez względu na stopień skomplikowania pytania. Gorąco polecam współpracę, gdyż macie pewność że nie zostaniecie potraktowani sloganami typu "oczywiście", "nie ma sprawy" tylko otrzymacie konkretną pomoc. Tak trzymać! Na pewno jeszcze nie raz skorzystam

Paweł
Studio Gama

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

Co robią referencje

W PHP referencje pozwalają na stworzenie dwu zmiennych zawierających tą samą zawartość. Więc poniższy skrypt:

<?php
$a
=& $b
?>

znaczy tyle, że $a oraz $b wskazują na tą samą zmienną.

Notatka: $a oraz $b są całkowicie równe, czyli nie $a wskazuje na $b lub odwrotnie, lecz $a oraz $b wskazują na to samo miejsce.

Ta sama składnia może być używana z funkcjami zwracającymi referencje, i z operatorem new (w PHP 4.0.4 i późniejszych):

<?php
$bar
=& new foo_klasa();
$foo =& znajdx_zmienna($bar);
?>

Notatka: Brak operatora & powoduje powstanie kopii obiektu. Jeżeli użyjesz $this wewnątrz klasy, zmienna ta będzie pracować na bierzącym egzemplarzu danej klasy. Dowiązanie bez użycia & spowoduje powstanie kopii egzemplarza danej klasy (czyli naszego obiektu) i $this będzie pracować na kopii, co nie jest zwykle tym czego byśmy chcieli. Zwykle pożądane jest posiadanie tylko jednego egzemplarza z którym chcemy pracować, z powodów wydajności i zajętości pamięci.

Pomimo tego, że możliwe jest użycie operatora @ do wyciszenia ewentualnych błędów w konstruktorze podczas użycia go w konstrukcji @new, to zapis ten nie działa kiedy używana jest konstrukcja &new. Jest to ograniczenie silnika Zend (Zend Engine) i użycie takiego zapisu spowoduje błąd parsowania.

Drugą rzeczą na którą pozwalają referencje jest przekazywanie zmiennych przez-referencje. Możliwe jest to przez uczynienie zmiennej lokalnej w funkcji i zmiennej w zakresie wywoływania tej funkcji odwołujacych się do tej samej wartości. Na przykład:

<?php
function foo (&$zmienna)
{
  
$zmienna++;
}

$a=5;
foo ($a);
?>

spowoduje, że $a będzie równe 6. Dzieje się tak, ponieważ w funkcji foo zmienna $zmienna odwołuje się do tej samaj zawartości jak zmienna $a. Zobacz również bardiej dokładne wyjaśniena na temat przekazywania przez referencję.

Trzecią rzeczą którą umożliwiają referencje jest zwracanie przez referencje.




User Contributed Notes

chat~kaptain524 at neverbox dot com
24-Aug-2005 06:15

Example 21-3 says that using references in "complex arrays" may not work as expected.  But I have found a way around it, to get normal reference behavior.

Compare the output of these two scripts:

<?php
for ( $i = 0; $i < 10; $i ++ ) $top[$i] = &$top;
$top['A'] = 'AAAAA'; var_dump( $top );
?>

<?php
$z
= &$top;
for (
$i = 0; $i < 10; $i ++ ) $top[$i] = &$top;
$top['A'] = 'AAAAA'; var_dump( $top );
?>

Running PHP 5.0, the first generates about 157457 lines while the second only generates 2554 lines.  The only difference in the code is setting an initial reference before adding self-references inside the array.  This simple step prevents the array from being duplicated every time another self-reference is added.

I am not sure if the duplication is intentional or if it's a bug in the way references are created, but the docs certainly recognize it.


+CurTis-
23-May-2005 09:15

I know that most everyones' comments are really insightful and many have exceptional PHP code to share, but I just wanted to point out a nice little method for situations where you are not sure what your variables contain exactly.

When developing, and you hit a wall debugging, be sure to echo your variable's contents at various stages of your script, so you can more easily track down where your problem is occurring. This is especially helpful when in a complicated referencing situation (like ladoo at gmx dot at's situation below). Also, be sure to make use of print_r, it is a life-saver!

Take care,
+CurTis-


curtis
23-May-2005 09:13

I know that most everyones' comments are really insightful and many have exceptional PHP code to share, but I just wanted to point out a nice little method for situations where you are not sure what your variables contain exactly.

When developing, and you hit a wall debugging, be sure to echo your variable's contents at various stages of your script, so you can more easily track down where your problem is occurring. This is especially helpful when in a complicated referencing situation (like ladoo at gmx dot at's situation below). Also, be sure to make use of print_r, it is a life-saver!

Take care,
+CurTis-


curtis
23-May-2005 09:12

I know that most everyones' comments are really insightful and many have exceptional PHP code to share, but I just wanted to point out a nice little method for situations where you are not sure what your variables contain exactly.

When developing, and you hit a wall debugging, be sure to echo your variable's contents at various stages of your script, so you can more easily track down where your problem is occurring. This is especially helpful when in a complicated referencing situation (like ladoo at gmx dot at's situation below). Also, be sure to make use of print_r, it is a life-saver!

Take care,
+CurTis-


ladoo at gmx dot at
17-Apr-2005 11:05

I ran into something when using an expanded version of the example of pbaltz at NO_SPAM dot cs dot NO_SPAM dot wisc dot edu below.
This could be somewhat confusing although it is perfectly clear if you have read the manual carfully. It makes the fact that references always point to the content of a variable perfectly clear (at least to me).

<?php
$a
= 1;
$c = 2;
$b =& $a; // $b points to 1
$a =& $c; // $a points now to 2, but $b still to 1;
echo $a, " ", $b;
// Output: 2 1
?>


miqrogroove
21-Mar-2005 10:40

More on references and globals:

String variables are not automatically passed by reference in PHP.  Some other languages, such as Visual Basic, will do that automatically for all variables.  PHP will not.

Consider the case where a function receives a global variable as a parameter and also has the same variable defined locally with the 'global' statement.  Do the parameter and global variable now reference the same memory?

No, they do not!  Passing the global variable into the function as a parameter caused a second variable to be created by default.

Here is an example of this scenario:

<?php

$teststring
="Hello";
one();

function
one(){
   global
$teststring;
  
two($teststring);
   echo
$teststring// The value is "World"
}

function
two($param){
   global
$teststring;
  
$teststring="World";
   echo
$param// The value is "Hello"
  
$param="Clear?";
}

?>

Enjoy


x123

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt