This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Objects in PHP Forum

View Course

245 points
Picture
Submitted by
Sam Gibb
almost 2 years ago

Parse error: PHP Objects 5/10

I keep getting the following error message "Parse error: syntax error, unexpected T_PUBLIC on line 16", it refers to the following line:

public function __construct ($firstname,$lastname,$age) {

What am I doing wrong?


0 votes

permalink

I'm a php noob myself, but could it be that you didnt end the previous line with a semicolon?

65 points
C7b7cdb99acddbcb28609414d801f6dc?s=140&d=retro
Submitted by
TypoNerd
almost 2 years ago


0 votes

permalink

Keep entering your code and the errors will probably go away as you enter more data - and be sure that you have the ; at the end of each line.

177 points
14d85c3a43d8e06f44eeb621eb3b2198?s=140&d=retro
Submitted by
John Ewing
almost 2 years ago


0 votes

permalink

I was getting the same error on this section too Sam. I believe I figured it out... (although my terminology may be messed up! See comments in code below).

Basically, I didn't notice that the __construct() and the method() are defined INSIDE the class!

ie.

class Person { // Class starts here.
        public $isAlive = true;
        public $firstname;
        public $lastname;
        public $age; 
            // Notice there's no closing curly } here.

        public  function __construct($firstname, $lastname, $age) { // __construct here
    $this->firstname =  $firstname;
    $this->lastname =  $lastname;
    $this->age =  $age;
        }

        public function greet() { // method here
        echo "Hello, my name is " . $this->firstname . " " . $this->lastname . " ". "Nice to meet you! :-)"; 
        }

    }  // Class end } is here!!

I hope this helps,

Dave

243 points
46c51f5da53e8817d66d431f4303376d?s=140&d=retro
Submitted by
davematthews
almost 2 years ago

2 Comments

Picture Sam Gibb almost 2 years ago

Thanks Dave, I definitely had the "}" in the wrong place. Still not working, not echoing now, full code below

46c51f5da53e8817d66d431f4303376d?s=140&d=retro davematthews over 1 year ago

It's still the }'s here Sam.
The two }} should be after the construct, just before you create the new Person instances ($teacher and $student)


0 votes

permalink

// <?php
// class Person {
// public $isAlive = true;
// public $firstname;
// public $lastname;
// public $age;
//

// public function __construct ($firstname,$lastname,$age) {
// $this-> firstname = $firstname;
// $this -> lastname = $lastname;
// $this -> age = $age;
//

// $teacher = new Person("boring", "12345","12345");
// $student = new Person("noone","important","12345");
//

// echo $student -> age;
// }
// }
//

// ?>
// <!-- Your code here -->
//
//

245 points
Picture
Submitted by
Sam Gibb
almost 2 years ago


0 votes

permalink

Hi Sam,

 <?php
    class Person {
        public $isAlive  = true;
        public $firstname;
        public $lastname;
        public $age;

    public function __construct($firstname, $lastname, $age) {
        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->age = $age;
        }
    }


        $teacher = new Person("boring", "12345", 12345);
        $student = new Person("George", "Hatzi", 34);

        echo $student->age;

    ?>
  </p>

Your }} are in the wrong place. Place them after the function.

237 points
Addfbf280f062172dcff6eb0851719e9?s=140&d=retro
Submitted by
George Hatzi
over 1 year ago


0 votes

permalink

*Oops, try again. Hey, did you forget add my name as a property to the object? :-P
i get this error and i don't know why?
Here is my code:
<!-- Your code here -->
<?php

     class Person {

         public $isAlive=true;
         public $firstname;
         public $lastname;
         public $age;

         public function __construct($firstname,$lastname,$age){
             $this->firstname = $firstname;
             $this->lastname = $lastname;
             $this->age = $age;

             }

         }

        $teacher = new Person("gigi","gsagas",24);
        $student = new Person("your","Gerald",19);



        echo $student->age;


    ?>**

605 points
Picture
Submitted by
xaero29
over 1 year ago