PHP Variable in a Class array -
i trying call variable add array within class, keenp getting unexpected t_variable error when 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 set within class or outside, doesn't matter. set pull database.
default variable values need compile time literals (they need constant before script runs, basically, "literal string"
, number 42
or array of constant values array(1, 2, 3)
), meaning, can't have dynamic value (such variable).
a better way use constructor:
protected $sourcecredentials = []; //php5.4 , above syntax, synonymous array() public function __construct(array $sourcecredentials) { $this->sourcecredentials = $sourcecredentials; }
note way, it's caller (the code instantiated object) pass in array of credentials outside. there different ways of doing so, considered best practice.
Comments
Post a Comment