0

I have a problem with accessing parents (non-static) property in child class static method. I've tried these as below:

class Parent
{
    protected $nonStatic;
    // Other methods and properties
}

class Child extends Parent
{
    public static function staticFunc()
    {
        $test = $this->nonStatic;     # Using $this when not in object context
        $test = self::$nonStatic;     # Access to undeclared static property
        $test = parent::$nonStatic    # Access to undeclared static property
    }
}

I checked similar question in stackoverflow, but i didn't get any working solution


P.S. Sorry about typos, and code above is a dummy example

Behzadsh
  • 851
  • 1
  • 14
  • 30
  • Your code, as provided, doesn't even compile. Please make sure that the code you give us actually fails in the same way you expect. – Charles Dec 16 '12 at 08:47
  • You have many errors here: not `protect $nonStatic;` but `protected $nonStatic;`, `$this` usage in static context... – Valera Leontyev Dec 16 '12 at 08:47
  • Well if you copied and pasted the code above from your actual code you have an issue with this `protect $nonStatic;` as it should be `protected $nonStatic;` Double check what you pasted. – PhearOfRayne Dec 16 '12 at 08:47
  • Property of what object are you trying to access? – zerkms Dec 16 '12 at 08:48
  • @Charles: in Child::staticFunc I just want to show all ways that I've tried and commented error I get by using each – Behzadsh Dec 16 '12 at 09:00
  • @Steven Farley and Valera Leontyev: sorry about typo :D, and I know I can't use $this in static methods, I just wanted to show all possible way – Behzadsh Dec 16 '12 at 09:01
  • I think you should first of all learn what static means. Looks like you're using that feature for something it has not been designed for. http://php.net/language.oop5.static – hakre Dec 16 '12 at 09:02
  • @zerkms: $nonStatic is property of Parent class and I wanted to use it in Child static method – Behzadsh Dec 16 '12 at 09:03
  • @Behzadsh: non-static properties belong to a particular objects, not classes – zerkms Dec 16 '12 at 09:03
  • @hakre: I know static method designed for what feature, I was changing a project patterns and getting error about this, I just ask to see is this possible or not – Behzadsh Dec 16 '12 at 09:07
  • 1
    If you know what static functions are, you already know the answer: Naturally it is *not* possible. Why do you ignore your knowledge and try and error anyway? And even then as your tests tell you that it does not work, you even create a question here on site and ask about it on top without sharing your knowledge upfront that you are sure that it is not possible but you would like to ask anyway if not there is a slight chance of some bug in the implementation in PHP that helps you work around (whatever your problem is). – hakre Dec 16 '12 at 09:12
  • @hakre: should I apologize you? then I'm really sorry :) – Behzadsh Dec 16 '12 at 09:20
  • More important is when you ask a question to make clear what you want to specifically ask about. That's all. Just do better next time :) And yes, PHP has an ill-sideeffect with which you could do that however, I don't think it's worth to propagate at this point. Just so you know. Better fix your design, remove all static. – hakre Dec 16 '12 at 09:22
  • 1
    Related with possible duplicate QA material: [How to access a private member inside a static function in PHP](http://stackoverflow.com/questions/1957629/how-to-access-a-private-member-inside-a-static-function-in-php); [could static members use nonstatic members and vice versa?](http://stackoverflow.com/questions/2204128/could-static-members-use-nonstatic-members-and-vice-versa) – hakre Dec 16 '12 at 09:39

3 Answers3

4

You cannot access to parent non-static property from a static method because it's not possible by definition and makes no sense.

Non-static properties are only available when you have an object instance, while you don't have any.

zerkms
  • 249,484
  • 69
  • 436
  • 539
1

Make parent's property static too. Otherwise It can't be accessed when you in static context.

Alexander Taver
  • 474
  • 3
  • 4
1

Obviously a static method won't know what a non-static parent property is. It doesn't know what instance of the object the call is being made on - so it can't know that objects parent. Either set the parent prop to static or pass in the instance of the child object to the method and call passedChildObject.parentProp

public static function staticFunc(Child c)
{
//should give you passed instance parent prop
return  c.$nonStatic
}

Now when you want the property..

{
//assume x is already initialized, this is just for clarity 
Child x;
returnedProp = x.staticFunc(x)
}
AssHat_
  • 353
  • 2
  • 13
  • thanks, as I said in question comments, I was changing a project design patterns, so I asked here if it possible or not, then by reading you answer I understand where I was wrong – Behzadsh Dec 16 '12 at 09:16