简体   繁体   中英

Best way to access Class property inside a Static method with PHP

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.

How should I handle this?

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

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.

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

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'];

Static methods in a class can't access the non static properties in the same class.

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.

If you wan't to access the properties of the same class then you must define them as a static.

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();

If possible, you could make your variable my_path static also.

self::my_paths['imagemagick'] does not work, because the array is private and could not used in a static context.

Make your variable static and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM