Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJason S
16,247 PointsWhy does name not need a dollar sign when it is being echoed?
echo $p->name;
why does this property not need a dollar sign?
Jason S
16,247 Pointsso the $ sign is only used for setting a property? also why do i have to create an instance of a class in order to echo it because I am unable to do it inside the foo class without creating $baz
michaelmugo
3,971 PointsBecause the class is just the schematics or blueprints of the object to be created.
It doesn't exist yet in the code, so technically you can't call the property because it doesn't exist. That's why we create the object from the class blueprint. This then creates the properties which can be called only through the created object.
The alternative is using the '$this' keyword in the class
2 Answers
Kenneth Hall
7,045 PointsHello again Jason,
Lets start with your 2nd question first, "Why do I have to create an instance of a class in order to echo it".
A class doesn't run on its own you have to create an instance of the class or as it more commonly referred to instantiate the class. When a class is instantiated you can then interact with the data by calling specific parts of the class. Check out the example below.
//Setting the person class
$class person
{
//This is where the property(which is just a variable inside an object) is set.
public $firstname = "";
}
//Setting an instance of the person class that is representing you.
$you = new person;
//Setting your first name to the $firstname property inside of the $you instance of the person class.
$you->firstname = 'Jason';
//printing the $firstname property of the $you instance of the person class.
echo $you->firstname; //Output: Jason
//Creating a new instance of the person class to represent me.
$me = new person;
//Setting my first name to the $firstname property inside of the $me instance of the person class.
$me->firstname = 'Kenny';
//printing the $firstname property of the $me instance of the person class.
echo $me->firstname; //output: Kenny
Now back to your first question. A property is just a variable that is set in a class. Let's use the example above. When we instantiate the class person inside the variable $you, we are creating a variable called $you then setting an instance of the class person inside that variable. Once the instance has been set the class will then be instantiated(or ran) when that variable is called. On the next line we then set the property $firstname which is inside the person class to your name. This is done by using a method arrow. A method arrow is used to access a method or property inside a class. The method arrow is why you don't need a dollar sign.
You are calling a property that is already set. It is just set inside the instance of the class you are in.
So $you->firstname = 'Jason'; is actually calling the person class then looking inside it for a property called $firstname then setting it to the string 'Jason'.
I hope this helps. Let me know if you need more explanation.
Jason S
16,247 Points-> replaces the need for $ yup thanks
Andrew Sheragy
16,379 PointsThinking back to basic algebra a variable is just a placeholder for some other value. In any code where you use a variable you could replace it with the actual value. For example in your case if $name = "Jason" then anywhere you see $name you can replace with "Jason" and your code would echo the same reulst. So the answer to your question is
Because
echo $p->"Jason";
Is not valid php syntax, then neither is
echo $p->$name;
Kenneth Hall
7,045 PointsKenneth Hall
7,045 PointsHello Jason,
It's because your are not accessing it like a variable, you are instantiating a class then calling the property of that class. Check out the example below for a bit more clarification.
In the example I set the class foo equal to the variable $baz then used to method arrow to call the property bar which is inside the foo object.
I hope this helps. Please let me know if you have any more questions or if you need further clarification.