b3tac0d3
b3tac0d3

Reputation: 908

php prepend a variable with data from another variable

I'm having an issue that seems like it shouldn't be an issue....I actually feel silly for not knowing this but I'm chalking it up to looking at the same code for way too long now...

I'm declaring some standard variables in my OOP class that should help me save a little space in other variables but I'm getting a T_VARIABLE error. Is there a different way I could do this? Thanks!

private $default_struc = array(
    "attributes" => array(
        "label" => "",
        "id" => "",
        "title" => "",
        "class" => "",
        "required" => "",
        "rel" => ""
    ), #end attributes
    "properties" => array(
        "disabled" => false
    ) #end properties 
);
private $input_struc = array(
    $this -> default_struc
);

Upvotes: 1

Views: 20

Answers (2)

b3tac0d3
b3tac0d3

Reputation: 908

So after reading up on the PHP docs, I solved the problem. I don't know if it's the best solution but here it is in case it helps anyone else.

private $default_struc = array(
    "attributes" => array(
        "label" => "",
        "id" => "",
        "title" => "",
        "class" => "",
        "required" => "",
        "rel" => ""
    ), #end attributes
    "properties" => array(
        "disabled" => false
    ) #end properties 
);
private $input_struc = array();

Since it's a class, I used my __construct to make it how I wanted.

function __construct($title, $action = "", $name = "", $rel = ""){
    $this -> input_struc = array_merge($this -> default_struc, $this -> input_struc);
        $this -> input_struc['attributes']['type'] = "";
        $this -> input_struc['attributes']['value'] = "";

Upvotes: 1

swidmann
swidmann

Reputation: 2792

Your problem is here:

private $input_struc = array(
    $this -> default_struc
);

You can just assign a direct value, not a value stored in another var or returned from a function/method, or concatenated,... Take a look at the php docs, there you will find a lot examples, where you can see, what's working or not

You can implement a getter method for this. Below is an example how it could look like:

// if you are using this only IN the class, you can keep it private
private function getInputStruc() {
    return array_merge(
        $this->default_struc,
        array(
            /*something*/
        )
    );
}

Upvotes: 1

Related Questions