7

I've this trait class:

trait Example
{
    protected $var;

    private static function printSomething()
    {
        print $var;
    }

    private static function doSomething()
    {
        // do something with $var
    }
}

And this class:

class NormalClass
{
    use Example;

    public function otherFunction()
    {
        $this->setVar($string);
    }

    public function setVar($string)
    {
        $this->var = $string;
    }
}

But i'm getting this error: Fatal error: Using $this when not in object context.

How can i solve this issue? I can't use properties on a trait class? Or this isn't really a good practice?

5
  • 2
    where/how are you calling setvar()? to get that error you'd have to be doing something like $foo = NormalClass::setVar() or whatever. and for your printSomething, $var would be an undefined local variable. Commented Aug 17, 2015 at 15:03
  • @MarcB i've updated my question. Commented Aug 17, 2015 at 15:05
  • 3
    that doesn't help, now it becomes "how/where are you calling otherFunction()"? You need to show the ENTIRE call chain. Commented Aug 17, 2015 at 15:05
  • You probably mean print static::$var;, since that method is static. Instance variables aren't going to help you there. Commented Aug 17, 2015 at 15:06
  • 5
    you are mixing instance-variables (which are part of the instance of a class) with class-variables (static, which are part of the class itself), $this->var VS. self::$var Commented Aug 17, 2015 at 15:06

2 Answers 2

20

Your problem is connected with differences between class's methods/properties and object's.

  1. If you define a property as static - you should access it through your class like classname/self/parent ::$property.
  2. If not static - then inside static property like $this->property.

For example:

trait Example   
{
    protected static $var;
    protected $var2;
    private static function printSomething()
    {
        print self::$var;
    }
    private function doSomething()
    {
        print $this->var2;
    }
}
class NormalClass
{
    use Example;
    public function otherFunction()
    {
        self::printSomething();
        $this->doSomething();
    }
    public function setVar($string, $string2)
    {
        self::$var = $string;
        $this->var2 = $string2;
    }
}
$obj = new NormalClass();
$obj -> setVar('first', 'second');
$obj -> otherFunction();

Static function printSomething can't access not static propertie $var! You should define them both not static, or both static.

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

1 Comment

@JoSSte please don't add meaningless edits
0

Using $this when not in object context because of this code

$this->form_validation->set_rules('username','Username','required');

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.