ajax - Basic PHP Class Creation Cause Parse Error: Parse Error


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


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.




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:

Parse error: syntax error, unexpected '{-code-1}', expecting function (T_FUNCTION) in test.php on line 11




It's always good to practice on simple examples to make its own idea about how it works.

This might help to clarify things.

class test
    public $x; 
    private $y; 

    function __construct() {
        echo "-- Constructor --<br/>";
        echo "-- Exiting Constructor --<br/>";

    public function changeX($val) {
        $this->x = $val;
        echo "X-->".$this->x."<br/>"; // for debugging purpose only

    private function changeY($val) {
        $this->y = $val;
        echo "Y-->".$this->y."<br/>"; // for debugging purpose only

    public function changeYprivate($val) {
        $this->changeY($val); // can call private method here

    public function getY() {
        return $this->y;

  $objTest = new test();

  echo "X is ".$objTest->x." and Y is ".$objTest->getY()."<br/>";
  $objTest->x = 10; // ok x is public, it can be modified

  // $a->changeY(4); // Error : cannot call this function outside the class !
  // $objTest->y = 20; // Error : y is private !
  // echo $objTest->y; // Error ! Can't even read y because it's private

  echo "X is ".$objTest->x." and Y is ".$objTest->getY()."<br/>";


-- Constructor --
-- Exiting Constructor --
X is 1 and Y is 2
X is 10 and Y is 4

People are also looking for solutions to the problem: php - How to send array of objects within request body in JSON format via Postman?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.