Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to call a variable to add to an array within a class, but I keenp getting unexpected T_VARIABLE error when I try.

class MB_API {
    $SName = 'Test';
    $PWD = 'test';
    $SiteID = '10';
    protected $client;
    protected $sourceCredentials = array(
        "SourceName"=>$SName, 
        "Password"=>$PWD, 
        "SiteIDs"=>array($SiteID)
    );
};

The variables can be set within the class or outside, it doesn't really matter. They will be set by a pull from a database.

share|improve this question
    
Would you edit your question to include the entire error message please? –  Don Cruickshank Mar 10 at 10:50

3 Answers 3

up vote 4 down vote accepted

Default variable values need to be compile time literals (They need to be constant before the script runs, basically, a "literal string", a number 42 or an array of constant values array(1, 2, 3)), meaning, they can't have a dynamic value (such as another variable).

A better way would be to use a constructor:

protected $sourceCredentials = []; //PHP5.4 and above syntax, synonymous to array()

public function __construct(array $sourceCredentials) {
    $this->sourceCredentials = $sourceCredentials;
}

Note that this way, it's up to the caller (the code that instantiated the object) to pass in the array of credentials from the outside. There are different ways of doing so, but this is considered best practice.

share|improve this answer
2  
To expand in a non technical way it means that $sourceCredentials can only be set as an empty array. You will have to fill it from within a method of MB_API –  Carl Markham Mar 10 at 10:31
    
@CarlMarkham: Expanded and added an example :) –  Second Rikudo Mar 10 at 10:34

You can assign your variable value in constructor.

For best practice you use like this:

class MB_API {

    private $Sname;
    private $PWD;
    private $SiteID;
    protected $client;
    protected $sourceCredentials;

    public function __construct() {

        // Set your default Values here

        $this->Sname = "Test";
        $this->PWD = 'test';
        $this->SiteID - '10';

        $this->sourceCredentials = array(
            "SourceName" => $this->SName,
            "Password" => $this->PWD,
            "SiteIDs" => array($this->SiteID)
        );
    }

}
share|improve this answer
    
Thanks this gave me what I was looking for. –  jak1502 Mar 10 at 13:20

You have two different "$sname" - one is $SName and another is $Sname

share|improve this answer
1  
PHP is case insensitive, that's not the problem. –  Second Rikudo Mar 10 at 10:34
    
Ahhh, so it is. Getting my languages confused! –  roycable Mar 10 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.