Reputation: 9169
I'm learning about classes and objects in PHP, and I'm getting really confused. This is what I have so far:
<?php
class ipInfo {
public $test1 = 'test';
}
$test = new ipInfo();
echo $test->$test1;
?>
Whenever I run it, I get these errors:
Notice: Undefined variable: test1 in //// on line 9
Fatal error: Cannot access empty property in //// on line 9
Upvotes: 5
Views: 9104
Reputation: 33542
Object properties don't need the second $
(unless you are using variable varibles).
echo $test->test1;
You use the $
to reference the variable and then the ->
to specify which propery you are looking at.
If you on the other hand have a variable with the value of test1
called $var
you could do this:
$var='test1';
echo $test->$var;
Which would work as the code would interpret the VALUE inside the $var and assume you meant that property.
Upvotes: 16