Felicia
Felicia

Reputation: 31

Unexpected T_VARIABLE, expecting T_FUNCTION fix

I am getting this syntax error when running my php. Here is the code for a class I am trying to costruct:

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);
    }
}

Anyone see whats wrong? Thanks!

Upvotes: 3

Views: 7105

Answers (2)

Kevin
Kevin

Reputation: 41885

You need to set the visibility of $secret

private $secret = "";

Then just remove that casting on the base64 and use $this->secret to access the property:

return base64_encode($this->secret);

So finally:

class mySimpleClass 
{

    // public $secret = "";
    private $secret = '';

    public function __construct($s) 
    {
        $this->secret = $s;
    }

    public function getSecret() 
    {
        return base64_encode($this->secret);
    }
}

Upvotes: 3

MH2K9
MH2K9

Reputation: 12039

I suggest you to declare $secret as public or private & access it using $this->. Example:

class mySimpleClass {
    public $secret = "";
    public function __construct($s) {
        $this -> secret = $s;
    }
    public function getSecret() {
        return base64_encode($this->$secret);
    }
}

Upvotes: 0

Related Questions