Here is my class property
这是我的班级财产
private $my_paths = array(
'imagemagick' => 'E:\Server\_ImageOptimize\ImageMagick',
'pngcrush' => 'E:\Server\_ImageOptimize\pngCrush\pngcrush.exe',
'jpegtran' => 'E:\Server\_ImageOptimize\jpegtran\jpegtran.exe',
'gifsicle' => 'E:\Server\_ImageOptimize\gifsicle\gifsicle.exe',
'pngquant' => 'E:\Server\_ImageOptimize\pngquant\pngquant.exe',
'pngout' => 'E:\Server\_ImageOptimize\pngout\pngout.exe'
);
There is a static method in the same class...
同一个类中有一个静态方法......
public static function is_image($file_path)
{
$imagemagick = $this->my_paths['imagemagick']. '\identify';
echo $imagemagick;
}
Of course this gives me errors like
当然这给我带来了错误
Fatal error: Using $this when not in object context...
I then tried accessing the property like this self::my_paths['imagemagick']
but that did not help.
然后我尝试访问该属性,如self :: my_paths ['imagemagick'],但这没有帮助。
How should I handle this?
我该怎么处理?
4 个解决方案
#1
24
You need the $ sign in front of the variable/property name, so it becomes:
您需要变量/属性名称前面的$符号,因此它变为:
self::$my_paths['imagemagick']
And my_paths is not declared as static. So you need it to be
并且my_paths未声明为静态。所以你需要它
private static $my_paths = array(...);
When it does not have the "static" keyword in front of it, it expects to be instantiated in an object.
当它前面没有“static”关键字时,它希望在一个对象中实例化。
#2
5
you cannot access non-static properties in static methods, you either should create an instance of the object in the method or declare the property as static.
您无法在静态方法中访问非静态属性,您应该在方法中创建对象的实例,或者将属性声明为静态。
#3
1
make it static property
使它成为静态属性
private static $my_paths = array(
'imagemagick' => 'E:\Server\_ImageOptimize\ImageMagick',
'pngcrush' => 'E:\Server\_ImageOptimize\pngCrush\pngcrush.exe',
'jpegtran' => 'E:\Server\_ImageOptimize\jpegtran\jpegtran.exe',
'gifsicle' => 'E:\Server\_ImageOptimize\gifsicle\gifsicle.exe',
'pngquant' => 'E:\Server\_ImageOptimize\pngquant\pngquant.exe',
'pngout' => 'E:\Server\_ImageOptimize\pngout\pngout.exe'
);
and call it like this
并称之为这样
self::$my_paths['pngcrush'];
#4
0
If possible, you could make your variable my_path static also.
如果可能,您也可以将变量my_path设为静态。
self::my_paths['imagemagick']
does not work, because the array is private and could not used in a static context.
self :: my_paths ['imagemagick']不起作用,因为该数组是私有的,无法在静态上下文中使用。
Make your variable static and it should work.
让你的变量静态,它应该工作。
#1
24
You need the $ sign in front of the variable/property name, so it becomes:
您需要变量/属性名称前面的$符号,因此它变为:
self::$my_paths['imagemagick']
And my_paths is not declared as static. So you need it to be
并且my_paths未声明为静态。所以你需要它
private static $my_paths = array(...);
When it does not have the "static" keyword in front of it, it expects to be instantiated in an object.
当它前面没有“static”关键字时,它希望在一个对象中实例化。
#2
5
you cannot access non-static properties in static methods, you either should create an instance of the object in the method or declare the property as static.
您无法在静态方法中访问非静态属性,您应该在方法中创建对象的实例,或者将属性声明为静态。
#3
1
make it static property
使它成为静态属性
private static $my_paths = array(
'imagemagick' => 'E:\Server\_ImageOptimize\ImageMagick',
'pngcrush' => 'E:\Server\_ImageOptimize\pngCrush\pngcrush.exe',
'jpegtran' => 'E:\Server\_ImageOptimize\jpegtran\jpegtran.exe',
'gifsicle' => 'E:\Server\_ImageOptimize\gifsicle\gifsicle.exe',
'pngquant' => 'E:\Server\_ImageOptimize\pngquant\pngquant.exe',
'pngout' => 'E:\Server\_ImageOptimize\pngout\pngout.exe'
);
and call it like this
并称之为这样
self::$my_paths['pngcrush'];
#4
0
If possible, you could make your variable my_path static also.
如果可能,您也可以将变量my_path设为静态。
self::my_paths['imagemagick']
does not work, because the array is private and could not used in a static context.
self :: my_paths ['imagemagick']不起作用,因为该数组是私有的,无法在静态上下文中使用。
Make your variable static and it should work.
让你的变量静态,它应该工作。