PHP fonksiyonu dışında değişkenler kullanılabilir hale?

10 Cevap

Ben değişkenleri bildirir bu işlevi vardır:

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;
}

Ben bu echo zaman:

   echo $image_1_width

Gayet iyi çalışıyor, ama alışkanlık değişkeni tanımıyor işlevi DIŞ yaparsanız, nasıl bir şekilde onları 'küresel' yapabilir?

Teşekkürler

10 Cevap

Ben kuvvetle NOT küresel kullanarak öneriyoruz.

Eğer işlevine dönmek için ne muhtemelen en iyi olacaktır:

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);

sonra her VAR erişebilirsiniz:

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

vb

Sen işlevinin dışında onları tanımlamak gerekir. Bunları kullanmadan önce ve işlevin içinde global anahtar sözcüğünü kullanın:

$someVar = null;

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

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

Bu gerçekten kötü bir tasarım seçimi olduğunu, olsa tavsiye edilir!

Ayrıca dönmek, sonra bir dizi ya da bir nesne oluşturmak olabilir - örneğin,

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

Daha sonra işlev kendisinde

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;
}

Ben kimse özü hakkında size düşündüm şaşırdım. Bir diziden değerleri alır ve yerel değişkenler içine dönecek. Yani, bu durumda:

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 );

Şimdi değişkenleri olacak,

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

Yerel kapsamında.

Bunun yerine bir işlevi daha bir sınıf uygulamak istiyorum sanki geliyor. Gibi bir şey:

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';

Tabii ki, inşaat bu tür, değişken isimlerini bir araya tutkal gerekmez. Sadece olabilir $image->width.

İkinci Lizard'ın cevap @ ben. Ayrıca, variable scope biraz yanlış gitmek değildir üzerinde okuma gibi geliyor. (Bu bağlantı, küresel değişkenleri kullanmak için nasıl bir açıklama içermez, Not. Burada birçok söylediler gibi, aşağı gitmek için not en iyi yol.)

yerine küresellerle kullanarak işlevin içinde değerini döndürür

Eğer $ GLOBALS dizisinin bir parçası olarak değişken bildirirseniz, küresel kapsam ekleyerek olacaktır.

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

Diğerleri de söylediğim gibi, globallerinin NULL, örneğin küresel kapsamında başlatıldı edilmelidir.

$var = 4;

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

Değişken küresel çünkü, 4 dönecektir. Bu istediğiniz ne olduğunu umuyoruz.