PHP sözdizimi hatası

1 Cevap php

Aşağıdaki ('=' beklenmeyen) bir sözdizimi hatası sonucu neden kimse bana açıklayabilir misiniz?

protected function processDates()
  {
       foreach($this->dates as $name => $unitprefix)     
       {
            $this->$unitprefix.'year' = '';   
            $this->$unitprefix.'month' = '';
            $this->$unitprefix.'day' = '';
       }
  }

Açıkçası ben boş bu değerleri terk edecek değilim ama devam etmeden önce, ben şimdiki sorunu düzeltmek gerekir.

Herhangi bir tavsiye takdir.

Teşekkürler.

1 Cevap

Denemek

$this->${$unitprefix.'year'} = '';

Ama daha iyi bir tavsiye vermek için bu sınıfın özelliklerini bilmek iyi olurdu ve ne $unitprefix içerir.

Referans: Variable variables

To give more details:
Your code is not clear to the parser in the way your write it. Assuming $unitprefix = 'foo', your code can be interpreted in two ways:

  1. $this->$unitprefix, yani $this->foo değerini alın ve append 'year'. Sonra kodu ($this->foo = bar ile) neden olacaktır:

    'baryear' = '';
    I guess this is what the parser is doing as this corresponds to an evaluation of the code from left to right.

  2. 'year' $unitprefix append ve çıkan kodu, yani sonuçta adı özelliği olsun:

    $this->fooyear = '';

İkinci sahip olmak istiyorum ama olmadan ${} ayrıştırıcı ne bilmiyor beviour olduğunu.