Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP访问静态方法内的类属性的最佳方法_Php_Oop - Fatal编程技术网

使用PHP访问静态方法内的类属性的最佳方法

使用PHP访问静态方法内的类属性的最佳方法,php,oop,Php,Oop,这是我的班级财产 private $my_paths = array( 'imagemagick' => 'E:\Server\_ImageOptimize\ImageMagick', 'pngcrush' => 'E:\Server\_ImageOptimize\pngCrush\pngcrush.exe', 'jpegtran' => 'E:\Server\_ImageOptimize\jpegtran\jpegtran.ex

这是我的班级财产

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'
);
同一类中有一个静态方法

public static function is_image($file_path)
{

    $imagemagick = $this->my_paths['imagemagick']. '\identify';

    echo $imagemagick;
}
当然,这给了我一些错误,比如

Fatal error: Using $this when not in object context...
然后,我尝试像这样访问属性
self::my_path['imagemagick']
,但没有任何帮助


我应该如何处理这个问题?

您不能在静态方法中访问非静态属性,您应该在方法中创建对象的实例,或者将属性声明为静态。

您需要在变量/属性名称前面加上
$
符号,这样它就会变成:

self::$my_paths['imagemagick']
并且
my_路径
未声明为静态路径。所以你需要它

private static $my_paths = array(...);

当它前面没有
static
关键字时,它希望在对象中实例化。

如果可能,您也可以将变量my_path设置为static

self::my_path['imagemagick']
不起作用,因为数组是私有的,无法在静态上下文中使用


将变量设为静态,它就会工作。

将其设为静态属性

   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'
   );
这样称呼它

   self::$my_paths['pngcrush'];

类中的静态方法无法访问同一类中的非静态属性

因为静态方法在没有对象实例的情况下是可调用的 创建时,虚拟变量
$this
在 方法声明为静态

如果不想访问同一类的属性,则必须将它们定义为静态

Example: 
class A{
    public function __construct(){
        echo "I am Constructor of Class A !!<br>";
    }

    public $testVar = "Testing variable of class A";

    static $myStaticVar = "Static variable of Class A ..!<br>";

    static function myStaticFunction(){

  //Following will generate an Fatal error: Uncaught Error: Using $this when not in object context 
      echo $this->testVar;

 //Correct way to access the variable..
    echo Self::$myStaticVar;

    }
}

$myObj = new A(); 
A::myStaticFunction();
示例:
甲级{
公共函数构造(){
echo“我是A类的构造函数!!
”; } public$testVar=“A类测试变量”; static$myStaticVar=“A类的静态变量..!
”; 静态函数myStaticFunction(){ //下面将生成一个致命错误:未捕获错误:不在对象上下文中时使用$this echo$this->testVar; //访问变量的正确方法。。 echo Self:$myStaticVar; } } $myObj=新的A(); A::myStaticFunction();
使它们成为静态的,并且
self
将起作用这是我的问题,当我尝试静态属性时缺少
$
,谢谢这是正确的答案,因为所选答案将抛出
PHP致命错误:访问未声明的静态属性