3

Here is my code:

class {
    $property = "something";
    public static function myfunc() {
         return $this->property;
    }
}

but PHP throws this:

Using $this when not in object context

I know, the problem is using $this-> in a static method, ok I remove it like this:

class {
    $property = "something";
    public static function myfunc() {
         return self::property;
    }
}

But sadly PHP throws this:

Undefined class constant 'property'

How can I access a property which is out of a static method in it?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • 5
    You can only access static properties or class constants from a static method, not instance properties; why does your method need to be static if you want it to access an instance property? – Mark Baker Dec 20 '16 at 11:43
  • @KhorneHoly thx, edited; – Martin AJ Dec 20 '16 at 11:43
  • @MarkBaker Because I need to call that method evey where without making an object of its class. – Martin AJ Dec 20 '16 at 11:45
  • 1
    If you don't want to instantiate the class, then don't make it an instance property, but a [static property](http://www.php.net/manual/en/language.oop5.static.php#language.oop5.static.properties) – Mark Baker Dec 20 '16 at 11:48
  • 2
    @sg- I'm one of those `idiots`. You don't explain a thing in that answer, you're just giving your shot to earn some easy rep with the lowest effort possible imho. Your code is correct, but I think that your answer is bad. – KhorneHoly Dec 20 '16 at 11:48
  • Possible duplicate of [Access a non-static property from a static method](http://stackoverflow.com/questions/15118495/access-a-non-static-property-from-a-static-method) –  Dec 20 '16 at 12:01

2 Answers2

7

Generally, you should not do it. Static methods don't have an access to instance fields for a reason. You can do something like this, though:

// define a static variable
private static $instance;

// somewhere in the constructor probably
self::$instance = $this;

// somewhere in your static method
self::$instance->methodToCall();

Note that it will work only for a single instance of your class, since static variables are shared between all instances (if any).

You'll also need to add a bunch of validations (e.g. is $instance null?) and pay attention to all the implementation details that may cause you some troubles.

Anyway, I don't recommend this approach. Use it at your own risk.

Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
2

Explaination

If you want to use a variable that wont change inside a class you don't want to instanciate, you need to use the static keyword in order to access it later in a method.

Also, you need a name for your class.

And finally, if you didn't specify a keyword as protected or public, you variable may be accessible outside the word, and so the method would be pointless. So I assume you need a protected value in order to use the method to call that variable.

Source-code

class Foo {

    protected static $property = 'something';

    public function getProperty() {

        return self::$property;

    }

}

echo Foo::getProperty(); /* will display : something */
echo Foo::$property; /* change from protected to public to use that syntax */

Documentation

PHP : classes.

PHP : static.

PHP : visibility.

Amin NAIRI
  • 2,292
  • 21
  • 20