这是我的代码=>
class Dbhead{
public static $category=array(
"id"=>"Id",
"title"=>"Title",
"code"=>"Code",
"description"=>"Description",
"remarks"=>"Remarks"
);
public static $client=array(
"id"=>"Id",
"title"=>"Title",
"name"=>"Name",
"mobile"=>"Mobile",
"address"=>"Address",
"remarks"=>"Remarks"
);
public $allfields=array(
"client"=>self::$client,
"category"=>self::$category
);
}
分配$client
& $category
数组到$allfields
,因为元素会破坏代码。
我尝试过更改$client
& $category
仅限公开。
我已经尝试了所有可能的方法来实现它,除了使用方法/函数做它,因为我不想这样做。
答案 0 :(得分:1)
你做不到。 manual says so。
作为一种解决方法,你可以这样做:
class Dbhead
{
public static $category = [
"id" => "Id",
"title" => "Title",
"code" => "Code",
"description" => "Description",
"remarks" => "Remarks",
];
public static $client = [
"id" => "Id",
"title" => "Title",
"name" => "Name",
"mobile" => "Mobile",
"address" => "Address",
"remarks" => "Remarks",
];
public static $allfields;
// Arguably not the most elegant way to solve the problem
// Since this is a setter without an argument
public static function setClient()
{
static::$allfields['client'] = static::$client;
}
public static function setCategory()
{
static::$allfields['category'] = static::$category;
}
}
或非静态的东西。你可以混合静态和非静态,但嘿,它不是那么好。
class DbHead{
protected $category, $client, $allFields;
public function __construct(array $category,array $client)
{
$this->category = $category;
$this->client = $client;
$this->allFields['client'] = $client;
$this->allFields['category'] = $category;
}
public function getCategory()
{
return $this->category;
}
public function getClient()
{
return $this->client;
}
public function getAllFields()
{
return $this->allFields;
}
// Alternatively provide setters for each field in particular
// If you don't wish to initialize the values on class instantiation
public function setCategory(array $category)
{
$this->category = $category;
return $this;
}
public function setClient(array $client)
{
$this->client = $client;
return $this;
}
public function createAllFields()
{
$this->allFields['client'] = $this->client;
$this->allFields['category'] = $this->category;
}
}
$dbHead = new DbHead([
"id" => "Id",
"title" => "Title",
"code" => "Code",
"description" => "Description",
"remarks" => "Remarks",
], [
"id" => "Id",
"title" => "Title",
"name" => "Name",
"mobile" => "Mobile",
"address" => "Address",
"remarks" => "Remarks",
]);
$dbHead->createAllFields();