0

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!

vertigoelectric
  • 1,307
  • 4
  • 17
  • 37
  • 2
    also, please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the [*red box*](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – tereško Sep 28 '12 at 01:16
  • Well this is a pretty big and already built website so I don't think switching all of its database functions to PDO or MySQLi is going to be ideal at this point. Then again, I'm not in charge. I'll have to take that up with my boss and see if he wants to pay me to take the time to do that. At any rate, you mentioned using an array. While I do understand arrays, I don't know how to implement what you're suggesting in this particular case. – vertigoelectric Sep 28 '12 at 01:21
  • Well .. the comment mentions "new code". Since you are saying it is a large and existing project, just keep it for the next one. – tereško Sep 28 '12 at 01:22
  • 2
    Oh , and try `var_dump($ob)` before the line that produces error .. though i still maintain that, if you have a class with no methods an all public variables, you should be using an array. – tereško Sep 28 '12 at 01:25
  • Well, it's not VERY large, but the point is that I get paid for my time so it's really not up to me what I spend time on, although the guy I work for is pretty cool about taking my recommendations. As far as your array idea... would it be possible to shed a little more light on how I could use an array in this situation? – vertigoelectric Sep 28 '12 at 01:25
  • [Why we don't use `global`](http://stackoverflow.com/a/11923384/508666). – PeeHaa Sep 28 '12 at 01:26

1 Answers1

1

Put

$clsLayoutDefinition = new LayoutDefinition;

inside the GetLayout function and end the function with:

 return $clsLayoutDefinition;

The caller should then do:

$clsLayoutDefinition = GetLayout(...);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you for this idea. I can see how it would work, however the function already returns something else. Although, I think I can define `$clsLayoutDefinition = new LayoutDefinition;` within the function without having to return it at the end. – vertigoelectric Sep 28 '12 at 16:57
  • I assumed that something outside the function needed it, since it was a global variable. If not, then that should work. – Barmar Sep 28 '12 at 16:58
  • Well, I noticed that `$clsLayout` is also used within that function, and I also noticed that there is `$clsLayout = new Layout;` defined just before it is used, so by doing the same with `$clsLayoutDefinition`, I figured it might help as you suggested... and so far it seems to have worked! I'm not sure if this will affect anything else in the site, but the warning has gone away. Thank you for this tip! I only needed the first part of it, though. – vertigoelectric Sep 28 '12 at 17:15