class Teste(){
public variavel;
public variavela;
function teste($parametro=$this->variavel, $parametro2->$this->variavela){
// code
}
}
I get the following error:
Parse error: syntax error, unexpected '$ this' (T_VARIABLE)
class Teste(){
public variavel;
public variavela;
function teste($parametro=$this->variavel, $parametro2->$this->variavela){
// code
}
}
I get the following error:
Parse error: syntax error, unexpected '$ this' (T_VARIABLE)
You can not do this, the default parameter values must be constant. The maximum that PHP allows is to use class constants, with static reference:
class Teste {
const VARIAVEL = 10;
const VARIAVELA = 20;
function __construct($parametro=self::VARIAVEL, $parametro2=self::VARIAVELA){
echo "$parametro - $parametro2";
}
}
$t = new Teste();
Although in this case it's worth putting the literal values directly into the function signature, is not it? It gets cleaner:
class Teste {
function __construct($parametro=10, $parametro2=20){
echo "$parametro - $parametro2";
}
}
$t = new Teste();
If you need a solution with dynamic values, assign the values within the function itself, as @ AndréRibeiro suggested in his response .
What you are trying to do is not valid in PHP.
According to the manual :
The default [of a function parameter] must be a constant expression, not (for example) a variable, a class member, or a function call.
An alternative with similar functionality:
class Teste(){
public $variavel;
public $variavela;
function teste($parametro = null, $parametro2 = null){
$parametro = $parametro ? $parametro : $this->variavel;
$parametro2 = $parametro2 ? $parametro2 : $this->variavela
}
}
In this example, if any of the parameters are not passed in the function call, the default value is the value of the class member.