2009-11-24 14 views
6

Ho questa funzione che dichiara variabili:rende le variabili disponibili al di fuori della funzione in PHP?

function imageSize($name, $nr, $category){ 
    $path = 'ad_images/'.$category.'/'.$name.'.jpg'; 
    $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg'; 
    list($width, $height) = getimagesize($path); 
    list($thumb_width, $thumb_height) = getimagesize($path_thumb); 
     ${'thumb_image_' . $nr . '_width'} = $thumb_width; 
     ${'thumb_image_' . $nr . '_height'} = $thumb_height; 
     ${'image_' . $nr . '_width'} = $width; 
     ${'image_' . $nr . '_height'} = $height; 
} 

Quando mi associo questo:

echo $image_1_width 

Funziona bene, ma se lo faccio fuori della funzione è solito riconoscere la variabile, come posso renderli 'globali' in qualche modo?

Grazie

risposta

13

Vorrei fortemente consigliare NON utilizzando globale.

Quale sarà probabilmente meglio è per voi di tornare dalla funzione:

function imageSize($name, $nr, $category){ 
    $path = 'ad_images/'.$category.'/'.$name.'.jpg'; 
    $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg'; 
    list($width, $height) = getimagesize($path); 
    list($thumb_width, $thumb_height) = getimagesize($path_thumb); 
     ${'thumb_image_' . $nr . '_width'} = $thumb_width; 
     ${'thumb_image_' . $nr . '_height'} = $thumb_height; 
     ${'image_' . $nr . '_width'} = $width; 
     ${'image_' . $nr . '_height'} = $height; 

    $myarr = array(); 
    $myarr['thumb_image_' . $nr . '_width'] = $thumb_width; 
    $myarr['thumb_image_' . $nr . '_height'] = $thumb_height; 
    $myarr['image_image_' . $nr . '_width'] = $width; 
    $myarr['image_image_' . $nr . '_height'] = $height; 
    return $myarr; 

} 

$myImage = imageSize($name, $nr, $category);

poi si accede ogni var:

echo $myImage['thumb_image_1_width']; 
echo $myImage['thumb_image_1_height']; 
echo $myImage['image_1_weight']; 
echo $myImage['image_1_height']; 

ecc

8

Si dovrà definire loro al di fuori della funzione. E all'interno della funzione utilizzare la parola chiave globale prima di usare loro:

$someVar = null; 

function SomeFunc() { 
    global $someVar; 
    // change $someVar 
} 

// somewhere later 
SomeFunc(); 
echo $someVar; 

essere avvisati però, che si tratta di una scelta di design davvero male!

+4

concordato. Suggerirei che la funzione ritorni quei valori come una matrice piuttosto che usare i globali. –

+0

perché è male? dammi anche degli argomenti ... –

+3

Se stai usando i globali non sai mai quando e chi li modificherà e interromperà il flusso del codice. Guarda alcuni dei suggerimenti in altre risposte per indizi su come riscrivere la tua funzione (suggerimento: restituisci le cose che ti servono) –

-1
$var = 4; 

function bla() 
{ 
    global $var; 
    return $var; 
} 

Si tornerà 4, perché la variabile è globale. Spero che questo sia quello che vuoi.

+1

return è molto diverso da echo. – davethegr8

0

ritorno il valore all'interno della funzione anziché utilizzare le globali

1

Sembra come se si desidera implementare una classe piuttosto che una funzione. Qualcosa di simile:

class myImage { 

    function __construct($name, $nr, $category){ 
     $path = 'ad_images/'.$category.'/'.$name.'.jpg'; 
     $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg'; 
     list($width, $height) = getimagesize($path); 
     list($thumb_width, $thumb_height) = getimagesize($path_thumb); 
     $this->{'thumb_image_' . $nr . '_width'} = $thumb_width; 
     $this->{'thumb_image_' . $nr . '_height'} = $thumb_height; 
     $this->{'image_' . $nr . '_width'} = $width; 
     $this->{'image_' . $nr . '_height'} = $height; 
    } 
} 

$image= new myImage($name, $nr, $category); 
echo $image->'image_1_width'; 

Naturalmente, con questo tipo di costruzione, non è necessario incollare nomi di variabili. Puoi semplicemente avere $image->width.

2

È anche possibile creare un array o un oggetto e restituirlo, ad es.

$dimensions[$nr] = imageSize($name,$category); 
echo "Thumb width " . $dimensions[$nr]['thumb_width']; 

Poi nella funzione stessa

function imageSize($name, $category) 
{ 
    $path = 'ad_images/'.$category.'/'.$name.'.jpg'; 
    $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg'; 
    list($width, $height) = getimagesize($path); 
    list($thumb_width, $thumb_height) = getimagesize($path_thumb); 

    $rsvp = Array(); 
    $rsvp['thumb_width'] = $thumb_width; 
    $rsvp['thumb_height'] = $thumb_height; 
    $rsvp['image_width'] = $width; 
    $rsvp['image_height'] = $height; 

    return $rsvp; 
} 
1

I secondi @ risposta di Lizard. Inoltre, suona come leggere su variable scope un po 'non andrebbe male. (Nota, questo link include la spiegazione di come utilizzare le variabili globali.) Come molti hanno già detto, è non la migliore strada da percorrere.)

2

Sono sorpreso che nessuno abbia pensato di parlarti dell'estratto . Prenderà i valori da una matrice e li trasformerà in variabili locali.Quindi, in questo caso:

function imageSize($name, $nr, $category){ 
    $path = 'ad_images/'.$category.'/'.$name.'.jpg'; 
    $path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg'; 

    $myarr = array(); 
    $myarr['thumb_image_' . $nr . '_width'] = $thumb_width; 
    $myarr['thumb_image_' . $nr . '_height'] = $thumb_height; 
    $myarr['image_image_' . $nr . '_width'] = $width; 
    $myarr['image_image_' . $nr . '_height'] = $height; 
    return $myarr; 

} 

$myImage = imageSize('myName', 'foo', $category); 
extract($myImage); 

Ora avrai le variabili,

$thumb_image_foo_width; 
$thumb_image_foo_height; 
$image_image_foo_width; 
$image_image_foo_height; 

in ambito locale.

+0

Ottimo punto! Non sapevo di 'extract()' prima. Ho sempre usato: 'while (lista ($ key, $ val) = each ($ array)) {$$ key = $ val;}'. + 1. – Kit

0

Se si dichiara la variabile come parte dell'array $ GLOBALS, la si aggiungerà all'ambito globale.

$GLOBALS['variable name'] = 'value'; 

Come altri hanno già detto, globali devono essere inizializzate nella portata globale, ad esempio NULL.

Problemi correlati