I have simple class and I want to set public
variable from out of class.
<?php
class AlachiqHelpers
{
public $height;
public static function getHeight($height)
{
return $this->height - 50;
}
public static function setHeight($height)
{
$this->height = $height;
}
}
In Result i get this error:
Using $this when not in object context
The
$this
keyword cannot be used under static context !.Case 1:
You need to remove the
static
keyword from the function defintion.Instead of
Should be
Case 2:
If you really need to make it(function) as
static
... You could just use theself
keyword to access the variable..Keep in mind that the
$height
variable is also madestatic
The working code.. (static one)
OUTPUT :
Remove
static
, these methods should not be static method but instance method.$this
can not be used under static context, because static context is shared by all the instances but not a single one.Static method can only access the static property.
Non-static method can access both non-static property (by
$this->foo
) and static property(byself::$foo
).Source
You can't use
$this
inside a static function, because static functions are independent of any instantiated object.Try making the function not static.