Php access property in static method

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?

asked Jan 9, 2012 at 8:32

JasonDavisJasonDavis

47.2k97 gold badges305 silver badges525 bronze badges

1

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.

answered Jan 9, 2012 at 8:36

1

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.

answered Jan 9, 2012 at 8:35

HeadshotaHeadshota

20.5k11 gold badges59 silver badges79 bronze badges

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

answered Jan 9, 2012 at 8:38

Php access property in static method

Ahmad HajjarAhmad Hajjar

1,7963 gold badges20 silver badges32 bronze badges

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.

answered Jan 9, 2012 at 8:37

Grrbrr404Grrbrr404

1,80915 silver badges17 bronze badges

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

answered Jan 11, 2020 at 8:36

Php access property in static method

Not the answer you're looking for? Browse other questions tagged php oop or ask your own question.

How do you access the class property from a static method?

Static properties ¶ Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ).

How can we call non

The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself. Similar post but different language.

Can we use this in static method PHP?

No special symbol is required to use this keyword. It is used with the scope resolution operator (::) of PHP. It does not refer to any instance of the class. It represents the static members of the class that are used by all class instances.

What are static methods and properties in PHP?

Introduction to PHP static methods and properties PHP allows you to access the methods and properties in the context of a class rather than an object. Such methods and properties are class methods and properties. Class methods and class properties are called static methods and properties.