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

Objects in PHP Forum

View Course » View Exercise

543 points
5372d1e7631fe97e0c0009e9_830860480
Submitted by
Corbin
almost 3 years ago

6/10 won't print out. What's wrong?

    <?php
        class Person {

        $teacher = new Person();
        $student = new Person();

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

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

    $teacher-> new Person("boring", "12345", 12345);
    $student-> new Person("your", "name", 99);

    echo $teacher->greet();
    echo $student->greet();
    ?>

It gives me this error:
"Parse error: syntax error, unexpected TVARIABLE, expecting TFUNCTION on line 13"


4 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;
        }

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

        $teacher = new Person ('boring', '12345', 12345);
        $student = new Person ('Franklin', 'Alvear', 29);

        echo $teacher->greet();
        echo $student->greet();
    ?>

541 points
53b966d952f863324e00021e_834676780
Submitted by
Franklin Alvear
almost 3 years ago


2 votes

permalink

I think
$teacher = new Person();
$student = new Person();

should not be in the class Person. And you have to replace the "->" with "=" in
$teacher-> new Person("boring", "12345", 12345);
$student-> new Person("your", "name", 99);
.

1125 points
53aede2f7c82ca3ec80019ed_767205995
Submitted by
Bela_T
almost 3 years ago

5 Comments

5372d1e7631fe97e0c0009e9_830860480 Corbin almost 3 years ago

It worked. Thank you! I don't know why the hints tell you to put "->" instead of "="

53aede2f7c82ca3ec80019ed_767205995 Bela_T almost 3 years ago

You're welcome!

53e36f7080ff3323f3001439_510830465 Rias Gremory almost 3 years ago

hmm u misread it.. Hint is clearly telling "="

53f6364a631fe968a80000c9_804808581 calebbradford almost 3 years ago

it won't work

53aede2f7c82ca3ec80019ed_767205995 Bela_T almost 3 years ago

@calebbradford It worked for me.


0 votes

permalink

<?php
class Person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
public $greet;
public function __construct($firstname, $lastname, $age)
$firstname -> prop1 = $prop1;
$lastname -> prop2 = $prop2;
$age -> prop3 = $prop3;

   }
   public function meth1("greet") {
       return "Hello, my name is Slim Shady";
   }

   return
   $teacher = new Person("boring", "12345", 12345); 
   $student = new Person("forename", "lastname", "age"); 
    echo $obj1 -> meth1("greet");
    echo $obj2 -> meth1("greet");
       ?>

worked in the first try :) you need to create a method that you call "greet"

352 points
Aa7135e06bd08ff7dc09bb94253065cb?s=140&d=retro
Submitted by
cnthnt777
almost 3 years ago


0 votes

permalink

this is the code i used to passed the lesson and print out the requests strings::

  <?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;

                }
               public function greet(){
                         return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
                    }
                }
                $teacher = new Person("boring", "12345", 12345);
                $student = new Person("lala", "yay", 25);

                echo $teacher->isAlive;

                echo $student->age;

                echo $teacher->greet();

                echo $student->greet();



        ?>

589 points
Picture
Submitted by
Ziana Nicole Mitchell
almost 3 years ago