There are numerous posts on the internet regarding this warning, and most of them have come to the same solution but that solution doesn't seem to be helping me. There's a lot of code involved, so I'll try to share the relevant bits and hopefully it's enough to come to a solution.
I have a PHP file that includes another PHP file. The included file contains the following declaration:
class LayoutDefinition
{
public $text;
public $enabled;
public $html_prefix;
public $html_suffix;
public $max_containers;
public $html_before_first;
public $html_between_each;
public $html_after_last;
public $layout_id;
}
//Create a new class instance
$clsLayoutDefinition = new LayoutDefinition;
Later on in the included file is the following function (with irrelevant code omitted):
function GetLayout($LayoutID, $AllDates = 0)
{
global $clsLayout;
//DB connection made here//
//Clear any previous data we have used.
unset($arrayLayout);
$query="..."; //db query set here//
$result = mysql_query($query);
if($result == FALSE)
{
trigger_error($query);
}
if(mysql_num_rows($result) == 0)
{
echo("GetLayout Query Error 1a - No results");
return;
}
$ob = mysql_fetch_object($result);
$clsLayoutDefinition->text = $ob->text; // LINE THAT GIVES ERROR
$clsLayoutDefinition->enabled = $ob->enabled;
$clsLayoutDefinition->html_prefix = $ob->html_prefix;
$clsLayoutDefinition->html_suffix = $ob->html_suffix;
$clsLayoutDefinition->max_containers = $ob->max_containers;
$clsLayoutDefinition->container_type = $ob->container_type;
$clsLayoutDefinition->html_before_first = $ob->html_before_first;
$clsLayoutDefinition->html_between_each = $ob->html_between_each;
$clsLayoutDefinition->html_after_last = $ob->html_after_last;
$clsLayoutDefinition->layout_id = $ob->id;
// more stuff blah blah blah
}
Now, in the original file there is a line that calls the GetLayout() function, passing a parameter. However, I get the "Creating default object from empty value" warning which comes from the line in the included file where I've noted above.
In the posts that I've seen regarding this kind of warning, the solution always seems to be that you must set a class or something to the variable name (in this case $clsLayoutDefinition) prior to assigning items to it. Unfortunately, I seem to be doing that here yet I am still receiving that warning.
Please keep in mind that I'm working with code that was originally written by someone else and I'm not yet too familiar with how classes/objects work.
I omitted some of the code for readability and relevance purposes, but if you think that there's something else at play here and need to see more of the code I can post some more. Just try to be specific if there's a particular part you need to see, if possible.
Anyway, this is work related so I hope I can find a solution quickly.
On a side note, when I originally encountered this issue days ago I 'solved' it by setting global $clsLayoutDefinition;
at the beginning of the GetLayout() function. However, tonight while working on the site I had encountered a really confusing problem and narrowed it down to that global declaration. When I remove the global declaration, the other (more serious) problem goes away, but then I get the warning.
Thanks in advance for any help!