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: 79
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Nie jestem webmasterem, ale i na mnie zrobiła wrażenie szybkość reakcji Darka na mój problem. Jego kompetencja i przede wszystkim zupełnie niemodna w dzisiejszych skomercjalizowanych czasach - zwykła ludzka życzliwość dla innego człowieka. Tacy ludzie to dziś gatunek niemal wymarły...

Leszek
Wojskowy Instytut Medyczny

   
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]

Zwracanie referencji

Zwracanie przez-referencję jest użyteczne gdy chcesz użyć funkcji do znalezienia zmiennej do której referencją powinna być dowiązana. Zwracając referencje używaj tej składni:

<?php
function &znajdz_zmienna ($param)
{
  
/* ...kod... */
  
return $znaleziona_zmienna;
}

$foo =& znajdz_zmienna ($bar);
$foo->x = 2;
?>

W tym przykładzie, będzie zmieniona własność obiektu zwróconego przez funkcję znajdz_zmienna, a nie własność kopii obiektu, jak to by było bez użycia składni referencji.

Notatka: Inaczej niż przy przekazywaniu parametrów, tutaj musisz używać & w obu miejscach - by wskazać, że zwracasz przez-referencję, a nie kopię jak normalnie, i by zwrócić uwagę, że dla zmiennej $foo powinno być użyte powiązanie przez referencję, a nie zwykłe przypisanie.




User Contributed Notes

warhog at warhog dot net
13-Dec-2005 09:04

firstly a note on the post below: technically correct that -> "to get a reference to an exsisting class and it's properties" should be "...to an existing object..."

in PHP5 it's senseless to return objects by reference.. let's say you have, as in the post below, a class which should return a reference to its own instance (maybe it's been created using the singleton pattern..), than it's no problem to simply return that variable holding the instance.
In PHP5 variables holding objects are always references to a specific object, so when you return (=copy) a variable "having" an object you in fact return a reference to that object.

Because of that behaviour, which is very common for many programming languages, especially those, which are to be compiled (due to memory problems and so on), you have to use the clone-function which was introduced in PHP5.

Here an example to underline what i mean:

<?php

class sample_singleton
{
 
protected static $instance = NULL;
 
 
private function __construct()
  { }

 
public static function create()
  {
self::$instance = new sample_singleton(); }

 
public static function getInstance()
  { return
self::$instance; }

 
public function yea()
  { echo
"wuow"; }
}

sample_singleton::create();

// $singleton will be a reference to sample_singleton::$instance
$singleton = sample_singleton::getInstance();

$singleton->yea();

?>

Note some more (maybe) strange behaviour: although $instance is private you can have a reference pointing on it. Maybe that does not seem strange to you in any way, but maybe you wondered as i did : )

The code posted here was just a sample to illustrate my posting, for using the singleton pattern please look it up in the manual. I just used this cause it was the simplest example which went in my mind right know.


willem at designhulp dot nl
09-Oct-2005 01:54

There is an important difference between php5 and php4 with references.

Lets say you have a class with a method called 'get_instance' to get a reference to an exsisting class and it's properties.

<?php
class mysql {
   function
get_instance(){
      
// check if object exsists
      
if(empty($_ENV['instances']['mysql'])){
          
// no object yet, create an object
          
$_ENV['instances']['mysql'] = new mysql;
       }
      
// return reference to object
      
$ref = &$_ENV['instances']['mysql'];
       return
$ref;
   }
}
?>

Now to get the exsisting object you can use
mysql::get_instance();

Though this works in php4 and in php5, but in php4 all data will be lost as if it is a new object while in php5 all properties in the object remain.


obscvresovl at NOSPAM dot hotmail dot com
24-Dec-2004 10:09

An example of returning references:

<?

$var
= 1;
$num = NULL;

function &
blah()
{
  
$var =& $GLOBALS["var"]; # the same as global $var;
  
$var++;
   return
$var;
}

$num = &blah();

echo
$num; # 2

blah();

echo
$num; # 3

?>

Note: if you take the & off from the function, the second echo will be 2, because without & the var $num contains its returning value and not its returning reference.


hawcue at yahoo dot com
17-Mar-2004 04:58

Be careful when using tinary operation condition?value1:value2

See the following code:

$a=1;
function &foo()
{
  global $a;
  return isset($a)?$a:null;
}
$b=&foo();
echo $b;  // shows 1
$b=2;
echo $a;  // shows 1 (not 2! because $b got a copy of $a)

To let $b be a reference to $a, use "if..then.." in the function.


contact at infopol dot fr
12-Feb-2004 06:36

A note about returning references embedded in non-reference arrays :

<?
$foo
;

function
bar () {
   global
$foo;
  
$return = array();
  
$return[] =& $foo;
   return
$return;
}

$foo = 1;
$foobar = bar();
$foobar[0] = 2;
echo
$foo;
?>

results in "2" because the reference is copied (pretty neat).


zayfod at yahoo dot com
03-Dec-2003 06:23

There is a small exception to the note on this page of the documentation. You do not have to use & to indicate that reference binding should be done when you assign to a value passed by reference the result of a function which returns by reference.

Consider the following two exaples:

<?php

function    & func_b ()
{
  
$some_var = 2;
   return
$some_var;
}

function   
func_a (& $param)
{
  
# $param is 1 here
  
$param = & func_b();
  
# $param is 2 here
}

$var = 1;
func_a($var);
# $var is still 1 here!!!

?>

The second example works as intended:

<?php

function    & func_b ()
{
  
$some_var = 2;
   return
$some_var;
}

function   
func_a (& $param)
{
  
# $param is 1 here
  
$param = func_b();
  
# $param is 2 here
}

$var = 1;
func_a($var);
# $var is 2 here as intended

?>

(Experienced with PHP 4.3.0)


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt