Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected T_VARIABLE, expecting T_FUNCTION fix

Tags:

oop

php

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!

like image 815
Felicia Avatar asked Feb 06 '26 23:02

Felicia


1 Answers

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);
    }
}
like image 89
Kevin Avatar answered Feb 08 '26 12:02

Kevin