5

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
2
  • 3
    Lose the $ in ->$test1 Commented Oct 18, 2013 at 1:47
  • 3
    This question appears to be off-topic because the right syntax can be found in the manual Commented Oct 18, 2013 at 1:59

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You dont access them with a $ sign:

try:

echo $test->test1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.