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

Objects in PHP Forum

View Course » View Exercise

871 points
510c4428e42722acec0048f8_54072012
Submitted by
Judy G.
almost 3 years ago

Error message with __construct

I get this error with my code, but __construct is there. Please help!

Oops, try again. I don't see your constructor: Is there a '__construct' 'function'?

//<?php
        class Dog {
            public $numLegs = 4;
            public $name;
            public function __construct($name) {
                $this->name = $name;
            }
        }
        ?>

3 votes

permalink

same problem here, and same for the 2 following exercises, anyone? :

<!DOCTYPE html>


Reconstructing the Person Class




<!-- 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("boring", "12345", 12345);

    $student = new Person("lea", "ma", 45);

    echo $teacher->isAlive;

    echo $student->age;



    ?>
  </p>
</body>

670 points
6998600a4dcac9aad67aba74d5349fa8?s=140&d=retro
Submitted by
leololo
almost 3 years ago

5 Comments

6998600a4dcac9aad67aba74d5349fa8?s=140&d=retro leololo almost 3 years ago

Parse error: syntax error, unexpected T_PUBLIC on line 18 AND same results in phpfiddle, so its not codeacademy, really wrong coding, even though I deid exactly what the instructions said tried variants..

5674c6dae39efec455000434_879409444 Zeke Y almost 3 years ago

Please create a new forum post so we can help you.

6998600a4dcac9aad67aba74d5349fa8?s=140&d=retro leololo almost 3 years ago

Thank you very much, found that I had to put the __construct method INSIDE the Person Class... rigtht ?

6998600a4dcac9aad67aba74d5349fa8?s=140&d=retro leololo almost 3 years ago

anyhow that works at least and was unsuccessful in finding out in google search to better understand, guess I m just too newby yet. Thank you very much for your attention Zeke !

5674c6dae39efec455000434_879409444 Zeke Y almost 3 years ago

Yes, I think putting it in the Person class is correct. Good luck with the rest of the course!


2 votes

permalink

your code has an error at line 2:
//<?php
it should be:
<?php

256 points
46f6fd3278ef8c319e17daeb32f7eadc?s=140&d=retro
Submitted by
Scratch45
almost 3 years ago


1 vote

permalink

Hi Judy,

Your code looks fine, have you tried refreshing the page and switching browsers?

2819 points
5674c6dae39efec455000434_879409444
Submitted by
Zeke Y
almost 3 years ago


0 votes

permalink

You probably wrote your construct statement with only 1 underscore instead of two. I made the same mistake. Found the answer in this other question: http://www.codecademy.com/forum_questions/53ecebe37c82cab537001db3

1060 points
5130e5ea5638e66d9a00d31a_403331339
Submitted by
Juan Rivera
almost 3 years ago

1 Comment

5674c6dae39efec455000434_879409444 Zeke Y almost 3 years ago

Nope. @Judy G. has two underscores.


0 votes

permalink

<?php
<!-- Your code here -->
class Dog {
public $numLegs = 4;
public $name;
public function __construct($name) {
$this->name = $name;
}
}

?>

618 points
22686a57dafc98d3ac407c56564b90b1?s=140&d=retro
Submitted by
Anita.Guni
about 2 years ago