I have the following class and its not accepting $this keyword in the method.
can someone guid
<?php
class test {
function __construct($x, $y, $z){
$this->$x = $x;
$this->testFunction();
public static function testFunction(){
print '<br> here it is:'.$this->$x.'--<br>';
}
//========================================================================================================================
}
?>
it gives me this error
Fatal error: Using $this when not in object context
In a static function, you need to use
self:$thisrefers to an object instance, which does not exist in a static context.That said, in a static context, the constructor will never have been called so
$xwill always be empty. I’m not sure whetherpublic static functionis really what you want here.Edit: Additionally, as @netcoder points out,
$xneeds to be declared a static member as well.