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

Object-Oriented PHP Forum

View Course » View Exercise

313 points
517f87fe2c0a457c42002ad9_28446780
Submitted by
S.Bip
over 4 years ago

Someone miss to define $name..... ? help me!

I think ... where is the definition of $name? (public $name; )

class Person {
public $isAlive = true;

function __construct($name) {
$this->name = $name;
}
public function dance() {
return "I'm dancing!";
}
}


2 votes

permalink

Maybe the confusion comes from the fact that the declaration

public $name;

known from previous examples is missing here...

587 points
53e7bfc0631fe9ff8800467d_249674865
Submitted by
piotr.zuk
about 3 years ago

1 Comment

Ff7f5591e3f60acac3214fe0703dcbae?s=140&d=retro jverbosk over 2 years ago

Yes, I was wondering the same thing, since that declaration has been made in all of the previous examples.

Looking around the web, it appears that the public properties (also called attributes) are independent of the __construct function's arguments. I found this page in particular to be helpful:

http://www.peachpit.com/articles/article.aspx?p=1949760&seqNum=5


1 vote

permalink

sweg

98 points
11483355060c99bd11986c965ad91256?s=140&d=retro
Submitted by
btapia
about 2 years ago


0 votes

permalink

thank you for reply, but... why this code run without error??
my question was.... can I tell about a var "name" if I didn't define it?

    class Person {
      public $isAlive = true;

      function __construct($name) {
          $this->name = $name;
      }

      public function dance() {
        return "I'm dancing!";
      }
    }

    $me = new Person("Shane");
    if (is_a($me, "Person")) {
      echo "I'm a person, ";
    }
    if (property_exists($me, "name")) {
      echo "I have a name, ";
    }
    if (method_exists($me, "dance")) {
      echo "and I know how to dance!";
    }
  ?>

313 points
517f87fe2c0a457c42002ad9_28446780
Submitted by
S.Bip
over 4 years ago

1 Comment

518556a4b6ad82a53e000a21_901691013 Zach Saucier over 4 years ago

From you're code it should run as it's suppose to (given you have <?php at the top. You can include the object's name by changing the second if statement to if (property_exists($me, "name")) { echo "I have a name, " . $me->name . ", "; }

This would output "I'm a person, I have a name, Shane, and I know how to dance!"


-1 votes

permalink

$name is not defined anywhere, which is why if (property_exists($me, "name") returns false and "I have a name, " is not printed. They left it out to show how property_exists tells whether the object has the property

457 points
518556a4b6ad82a53e000a21_901691013
Submitted by
Zach Saucier
over 4 years ago

1 Comment

51c044fd282ae30f2100af17_466026925 532970813@qq.com 白 about 4 years ago

I'm a person, I have a name, and I know how to dance!

so if (property_exists($me, "name") return true.


-1 votes

permalink

Although the property $name doesnt exist still Im getting "I have a name" in the output?Can you please explain.

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

      function __construct($name) {
          $this->name = $name;
      }

      public function dance() {
        return "I'm dancing!";
      }
    }

    $me = new Person("Shane");
    if (is_a($me, "Person")) {
      echo "I'm a person, ";
    }
    if (property_exists($me, "name")) {
      echo "I have a name, ";
    }
    if (method_exists($me, "dance")) {
      echo "and I know how to dance!";
    }
  ?>

Output -

I'm a person, I have a name, and I know how to dance!

73 points
3b40d9a525e2a1d299942207b05e3382?s=140&d=retro
Submitted by
Sana Farooqui
over 3 years ago

3 Comments

518556a4b6ad82a53e000a21_901691013 Zach Saucier over 3 years ago

What do you mean? It does exist. "function __construct($name) {
$this->name = $name;
}"

3b40d9a525e2a1d299942207b05e3382?s=140&d=retro Sana Farooqui over 3 years ago

Ok,I wasnt sure where it was defined.Thanks for the reply!

Picture Juan Diego Gonzales over 2 years ago

What you mean, @Zach, it's that you can define a property without doing "public $prop;" and just doing so by calling $prop in __construct?

Futhermore I'd like to know why exactly the "public $name;" statement is missing... it is unnecessary? What would be the "good practice"?