Erez.Matrix
Erez.Matrix

Reputation: 89

PHP WARNING: Creating default object from empty value - Confused

I've included errors and warnings in my PHP.INI and the last 2 errors were solved, they were mostly "Creating default object from empty value" - The issue is that I have no idea where to start here.

With the previous (same) error it was easier since there was a public class extended from the current and had an object instantiated so all it took was nesting the instance in an !isset condition.

Yet here

public function forward( $forward )
{
     $this->viewBean->_forward = $forward;
}

The error is pointing on the $this. There is no object instantiation and the method above is part of an Abstract Class identified as BaseController Since I can't instantiate an object from an Abstract Class, I truly have no idea how to continue to debug this PHP warning.

Please let me know if any info is amiss, I'll comment back

Upvotes: 0

Views: 144

Answers (1)

Jaquarh
Jaquarh

Reputation: 6693

Abstract classes are not supported in PHP like this.

You must store the Object as a local variable for it to 'remember' its data.

class Example
{
    public $abstract;
    public function forwards($forwards)
    {
        $this->abstract = $this->viewBean;
        $this->abstract->_forwards = $forwards;
    }
    public function viewBean()
    {
        return new OtherClass;
    }
}

// TODO: Add your other class with its Public variable

Example of usage:

// <-- for newer supported php versions -->
$exampleClass = (new Example)->forwards("add me!");
echo $exampleClass->abstract->_forwards;

// <-- for depreciated php versions -->
$exampleClass = new Example;
$exampleClass->forwards("add me!");
echo $exampleClass->abstract->_forwards;

Although this fixes your issue, I do not see why you'd want to ever forward a variable? You can use the extends to create child Classes which inherits the properties and methods held inside the context.

There is always callback methods you can use also.

Upvotes: 1

Related Questions