0

Why is PHP Saying that my object is empty ?

Here is a simple use case.
It keeps returning the following error :

Fatal error: Cannot access empty property in C:\AdvancedApp\myApp.php on line 10.

class myClass
{
    public $myFunction = "Hello World";
}

$class = new myClass();

echo $class->$myFunction;
1
  • 5
    no more $ sign on the property when accessing it, just echo $class->myFunction; alone, by the way, that name is misleading Commented Sep 17, 2014 at 8:32

2 Answers 2

3

The correct use of property is:

echo $class->myFunction;

What you did is used variable variables, the following will work:

$name = "myFunction" ;
echo $class->$name ;
Sign up to request clarification or add additional context in comments.

Comments

1

Drop the $ sign from $myClass->$myFunction so it will be $myClass->myFunction, and what's with the variable name. Use something else, like myValue...

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.