Ben yaratıyorum bir sınıfta çok garip bir sorunla karşılaştık. Aşağıda sınıfının bir parçası olduğunu ve bu çıkışı bulunuyor:
class WeirdHappenings
{
protected $filters_list = array();
...
function build()
{
$filters_count = count($this->filters_list);
echo "<pre>";
var_dump($this->filters_list);
echo "<br>" . $filters_count . " is the count";
echo "</pre>";
}
}
Sizden önce, evet filters_list sınıfının yürütülmesi sırasında doldurulan bir kalabalık dizidir. Vardump kanıtlıyor:
array(2) {
["filter_1"]=>
string(17) "calendar year nbr"
["filter_2"]=>
string(18) "reviewer type desc"
}
0 is the count
Bu nasıl mümkün olabilir? Henüz sayısı kadar büyük olduğunu söyleyemezsiniz iki unsur ile bir dizi var?
PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
error_reporting = E_ALL | E_STRICT
Bu çözünürlük:
class WeirdHappenings
{
protected $filters_list = array();
protected $thing = array("foo" => "bar", "ack" => "bar");
function WeirdHappenings()
{
}
function makeMeCry()
{
$filters = array();
$filter_count = 1;
$crapola = array("f1" => array("name" => "calendar year nbr"), "f2" => array("name" => "reviewer type desc"));
foreach( $crapola as $key => $data )
{
$filters["filter_$filter_count"] = $data['name'];
$filter_count++;
}
$this->filters_list = $filters;
}
function build()
{
$filters_count = count($this->filters_list);
$this->makeMeCry();
echo "<pre>";
var_dump($this->filters_list);
var_dump($this->thing);
echo "<br>" . $filters_count . " is the count of the filters";
echo "<br>" . count($this->thing) . " is the count of the thing";
echo "</pre>";
}
}
$weirdthings = new WeirdHappenings();
$weirdthings->build();
Sayısız açıklamalarda belirttiği gibi sayısı önceki dizinin nüfusa gerçekleştirildi ediliyordu.