Vishal Kumar Sahu
Vishal Kumar Sahu

Reputation: 1396

Assign Arrays as Array Elements to a PHP's Class Variable/Property

Here is my code =>

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

Assigning $client & $category arrays to $allfields as elements breaks the code. I have tried changing $client & $category to public only.

I have tried all the possible ways I knew to achieve it except doing it using method/function as I do not want that.

Upvotes: 1

Views: 824

Answers (1)

Andrei
Andrei

Reputation: 3588

You can't. The manual says so.

As a work-around you can do this:

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

Or non static stuff. You can mix static and non-static but hey, it's not that great.

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

Upvotes: 1

Related Questions