roelof Posted August 17, 2013 Share Posted August 17, 2013 Hello, I have to make a this task. 01. Add a public $isAlive property to the Person class and assign the value true to $isAlive. 02. Add three further public properties to thePerson class: $firstname, $lastname and $age. Don't assign any values to these. 03. echo the value of the $isAlive property of your $teacher object. So I made up this script : <!DOCTYPE html> <html> <head> <title>Reconstructing the Person Class</title> <link type='text/css' rel='stylesheet' href='style.css'/> </head> <body> <p> <?php class Person { public $isAlive = true; public $firstname ; public $lastname ; public $age; } $teacher = new Person(); $student = new Person(); echo teacher -> isAlive(); ?> </p> </body> </html> But it seems it do not output the value of the isAlive of the teacher . What did I do wrong ? Roelof Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted August 17, 2013 Share Posted August 17, 2013 $teacher = new Person(); echo teacher->isAlive(); Notice the difference? Quote Link to comment Share on other sites More sharing options...
roelof Posted August 17, 2013 Author Share Posted August 17, 2013 Thanks, I still see this message : Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' on line 18 Roelof Quote Link to comment Share on other sites More sharing options...
0xMatt Posted August 17, 2013 Share Posted August 17, 2013 Also, isAlive is not a function, so once you fix your first issue you'll immediately run into another. You should definitely try learning some php a bit more before you take dip into the objects pond. This is what you want though I have no idea why: <?php class Person { public $isAlive = true; public $firstname ; public $lastname ; public $age; } $teacher = new Person; $student = new Person; echo $teacher->isAlive; // prints 1 ?> Quote Link to comment Share on other sites More sharing options...
roelof Posted August 17, 2013 Author Share Posted August 17, 2013 Because CodeAcademy says this : Property Panic (1) Great work, now we can add some properties to our class. ;-) As you remember, properties are pieces of data bound to an object, and you can imagine an object as a bundle of information and actions. So here is the syntax for properties: class Classname { public $propname = "Some value"; public $otherProp As we want to add some public information, which can be accessed from everywhere, we use the public keyword (there is no var in objects in PHP 5). The first one, $propname, contains a string called "Some value", while the second one, $otherProp, doesn't contain anything (yet). To access the property $propname of an existing $obj object, we have to use the following syntax: $obj->propname; So in PHP there is no dot notation as in JS, Python and Ruby, but there is an arrow notation, as we use arrows instead of dots in PHP. ;-) Roelof Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.