ajax - Basic PHP Class Creation Cause Parse Error: Parse Error
<?php
class TEST{
public $x=1;
private $y=2;
public function changeA($val){
//$this->x = $val;
echo "X-->".$this->x;
}
private function changeB($val){
//$this->y = $val;
echo "Y-->".$this->y;
}
}
$a = new TEST();
$a->changeA(3);
#
$a->changeB(4);
This is really bugging me, I use all the correct syntax but I got error right on the line I doCLASS test
.
Parse error: parse error in file.php on line x
Tested: -Remove vars, functions, new objects. nothing fix it.
====Updated Code above, but still, the same error.
I think there is something wrong with my php... I am now running all different kind of code, even echo return the same error. I think there is some other trouble with my php setup.
===Last update
I was using Ajax to passing value and write into a php file with 755 and public access. It was seem like a kind of process hiccup. Now it functioning correctly. But the example still, really useful. Well, don't know what is the vote down for, its seem make sense to mark reason for vote down as well like the ones who need to vote to close it. So SO can as least know the reason for the vote down. Interesting right? Someone who actually care about improving this.
Answer
Solution:
Class method definitions are not statements, and therefore should not be terminated with
{-code-1}
.This means that the
}{-code-1}
on lines 11, 16 and 17 should simply be}
instead.On another note, I don't know what version of PHP you're using. I'm using PHP 5.5 and got a very clear message:
Answer
Solution:
It's always good to practice on simple examples to make its own idea about how it works.
This might help to clarify things.
Output: