Error Unexpected T_variable Expecting T_function
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more expecting function (t_function) php about Stack Overflow the company Business Learn more about hiring developers or posting ads
Syntax Error, Unexpected '$data' (t_variable), Expecting Function (t_function)
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
Syntax Error, Unexpected '$this' T_variable
is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Unexpected T_VARIABLE, expecting T_FUNCTION fix up vote 2 down vote favorite I am
Parse Error: Syntax Error, Unexpected T_variable In Php
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! php oop share|improve this question edited Nov 16 '14 syntax error, unexpected '->' (t_object_operator) at 1:47 Ghost 35.3k122545 asked Nov 15 '14 at 7:17 Felicia 112 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote 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); } } share|improve this answer edited Nov 15 '14 at 8:14 answered Nov 15 '14 at 7:19 Ghost 35.3k122545 that seemed to work! thanks! –Felicia Nov 15 '14 at 7:22 @Felicia sure, im glad this helped –Ghost Nov 15 '14 at 7:23 so it is supposed to echo mySimpleClass Object ( [secret:mySimpleClass:private] => Honey Pot ) sg9uzxkgug90 but it only echos mySimpleClass Object ( [secret] => Honey Pot ) –Felicia Nov 15 '14 at 7:49 @Felicia you need to echo it from your getter method. echo $obj->getSecret() –Ghost Nov 15 '14 at 7:57 1 Aye good, no need for the $secret to be public at this point. –Cliffordlife Nov 15 '14 at 8:13 | s
Forum View Course 176 points Submitted by James Barclay about 3 years ago Confusing error with PHP objects - Unexpected T_VARIABLE, expecting T_FUNCTION Whenever I http://stackoverflow.com/questions/26943573/unexpected-t-variable-expecting-t-function-fix create a new instance of an object I receive this error: Parse error: syntax error, unexpected TVARIABLE, expecting TFUNCTION on line ...(in the example to come, line 10) The code passes https://www.codecademy.com/en/forum_questions/5211e84af10c607d6b000ff0 each task, however I just wanted to know whether this is actually a legitimate error in how I'm writing the code or whether it is a error not related to me. Below is the simplest code I have done that still produces the error.
` 1 vote permalink You're supposed to create the instance outside of the class: class Person{ } $me = new Person(); 322 points Submitted by DM about 3 years ago