PHPで次のデータ構造を定義することは可能ですか?
typedef struct
{
int maxrank;
float rankrat[maxpop];
int rankno[maxpop];
individual ind[maxpop],
*ind_ptr;
}population ;
population pop;
PHPで次のデータ構造を定義することは可能ですか?
typedef struct
{
int maxrank;
float rankrat[maxpop];
int rankno[maxpop];
individual ind[maxpop],
*ind_ptr;
}population ;
population pop;
class
PHP ではa 以外のデータ構造を定義できないため、いつでも実行できます。次のようになります。
class Population
{
public $maxrank;
public $rankrat = array();
public $rankno = array();
public $ind = array();
public $ind_ptr;
}
$population = new Population();
データ構造をもう少し見てみると、とIndividual
から参照されるクラスも必要になります。PHP は緩やかに型付けされているため、すぐにやその他のものになる可能性があることを覚えておいてください。これを回避する簡単な方法は、次のようなタイプ ヒント セッター メソッド (およびその後のゲッター) を使用することです。$population->ind_ptr
$population->ind[]
$ind_ptr
array
string
protected $_ind_ptr;
public function setIndPtr(Individual $ind)
{
$this->_ind_ptr = $ind;
}
public function getIndPtr()
{
return $this->_ind_ptr;
}
または、配列が 1 つの場所にしかない場合は、配列を使用して定義します。
はい、クラスで。
class Population
{
public $maxrank;
public $rankrat = array();
public $rankno = array();
}