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
3 个解决方案
#1
6
The $this
keyword cannot be used under static context !.
Case 1:
You need to remove the static
keyword from the function defintion.
您需要从函数定义中删除static关键字。
Instead of
public static function setHeight( $height ){
Should be
public function setHeight( $height ){
Case 2:
If you really need to make it(function) as static
... You could just use the self
keyword to access the variable..
如果你真的需要将它(函数)作为静态...你可以使用self关键字来访问变量..
public static $height;
public static function setHeight( $height )
{
self::$height=22;
}
Keep in mind that the $height
variable is also made static
请记住,$ height变量也是静态的
The working code.. (static one)
<?php
class AlachiqHelpers
{
public static $height;
public function getHeight()
{
return self::$height - 50;
}
public static function setHeight($height1)
{
self::$height = $height1;
}
}
$a = new AlachiqHelpers();
$a->setHeight(180);
echo $a->getHeight();
OUTPUT :
130
#2
3
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.
$ this不能在静态上下文中使用,因为静态上下文由所有实例共享,但不是单个实例共享。
Static method can only access the static property.
静态方法只能访问静态属性。
Non-static method can access both non-static property (by $this->foo
) and static property(by self::$foo
).
非静态方法可以访问非静态属性(通过$ this-> foo)和静态属性(通过self :: $ foo)。
#3
0
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
因为静态方法可以在没有创建对象实例的情况下调用,所以伪变量$ this在声明为static的方法中不可用。
You can't use $this
inside a static function, because static functions are independent of any instantiated object.
你不能在静态函数中使用$ this,因为静态函数独立于任何实例化对象。
Try making the function not static.
尝试使该功能不是静态的。
public function setHeight( $height ){
$this->height=$height;
}
#1
6
The $this
keyword cannot be used under static context !.
Case 1:
You need to remove the static
keyword from the function defintion.
您需要从函数定义中删除static关键字。
Instead of
public static function setHeight( $height ){
Should be
public function setHeight( $height ){
Case 2:
If you really need to make it(function) as static
... You could just use the self
keyword to access the variable..
如果你真的需要将它(函数)作为静态...你可以使用self关键字来访问变量..
public static $height;
public static function setHeight( $height )
{
self::$height=22;
}
Keep in mind that the $height
variable is also made static
请记住,$ height变量也是静态的
The working code.. (static one)
<?php
class AlachiqHelpers
{
public static $height;
public function getHeight()
{
return self::$height - 50;
}
public static function setHeight($height1)
{
self::$height = $height1;
}
}
$a = new AlachiqHelpers();
$a->setHeight(180);
echo $a->getHeight();
OUTPUT :
130
#2
3
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.
$ this不能在静态上下文中使用,因为静态上下文由所有实例共享,但不是单个实例共享。
Static method can only access the static property.
静态方法只能访问静态属性。
Non-static method can access both non-static property (by $this->foo
) and static property(by self::$foo
).
非静态方法可以访问非静态属性(通过$ this-> foo)和静态属性(通过self :: $ foo)。
#3
0
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
因为静态方法可以在没有创建对象实例的情况下调用,所以伪变量$ this在声明为static的方法中不可用。
You can't use $this
inside a static function, because static functions are independent of any instantiated object.
你不能在静态函数中使用$ this,因为静态函数独立于任何实例化对象。
Try making the function not static.
尝试使该功能不是静态的。
public function setHeight( $height ){
$this->height=$height;
}