Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/281279-codeacademy-help/
Share on other sites

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
?>
Link to comment
https://forums.phpfreaks.com/topic/281279-codeacademy-help/#findComment-1445529
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/281279-codeacademy-help/#findComment-1445530
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.