运行我的php时出现此语法错误。这是我正在尝试构建的类的代码:
function makeObject($s) {
$secobj = new mySimpleClass($s);
return $secobj;
}
class mySimpleClass {
$secret = "";
public function __construct($s) {
$this -> secret = $s;
}
public function getSecret() {
return base64_encode(string $secret);
}
}
有人看到什么错了吗?谢谢!
答案 0 :(得分:3)
您需要设置$secret
private $secret = "";
然后在base64上删除该转换,并使用$this->secret
访问该属性:
return base64_encode($this->secret);
最后:
class mySimpleClass
{
// public $secret = "";
private $secret = '';
public function __construct($s)
{
$this->secret = $s;
}
public function getSecret()
{
return base64_encode($this->secret);
}
}
答案 1 :(得分:0)
我建议您将$secret
声明为public
或private
&使用$this->
访问它。例如:
class mySimpleClass {
public $secret = "";
public function __construct($s) {
$this -> secret = $s;
}
public function getSecret() {
return base64_encode($this->$secret);
}
}