How to access a private member inside a static function in PHP?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

Class MyClass { private static $MyMember = 99; public static function MyFunction() { echo self::$MyMember; } } MyClass::MyFunction() see Visibility and Scope Resolution Operator (::) in the oop5 chapter of the php manual.

Class MyClass { private static $MyMember = 99; public static function MyFunction() { echo self::$MyMember; } } MyClass::MyFunction(); see Visibility and Scope Resolution Operator (::) in the oop5 chapter of the php manual.

1 - I Didn't know about self keyword. Your code worked! Thanks :) – anon355079 Dec 24 '09 at 10:02.

Within static methods, you can't call variable using $this because static methods are called outside an "instance context". It is clearly stated in the PHP doc.

– anon355079 Dec 24 '09 at 10:17 yes you can, but maybe you shouldn't since it obscures the fact, that the function is object-bound .. – miku Dec 24 '09 at 10:24 Weird. I thought you can only call static functions by ClassName::FunctionName and not by instantiating. Anyway, I have this doubt -> If you declare the variable like in case A) and use it like echo $MyMember, it is not working for me.

It shouldn't work right? I am unable to understand your comment - //local there. – anon355079 Dec 24 '09 at 10:28 as far as I know, when you use "echo $MyMember;", it refers to the "local scope" (here, the function) of the variable, and since we haven't defined any "$MyMember" inside the function, this line yields "nothing" ... – miku Dec 24 '09 at 10:32 oh!

Since you didn't mention any fatal error or something, I thought it will work :D. +1 for taking time to explain all the cases :) – anon355079 Dec 24 '09 at 10:34.

This is a super late response but it may help someone.. class MyClass { private $MyMember; public static function MyFunction($class) { $class->MyMember = 0; } } That works. You can access the private member that way, but if you had $class you should just make MyFunction a method of the class, as you would just call $class->MyFunction(). However you could have a static array that each instance is added to in the class constructor which this static function could access and iterate through, updating all the instances.Ie.. class MyClass { private $MyMember; private static $MyClasses; public function __construct() { MyClass::$MyClasses = $this; } public static function MyFunction() { foreach(MyClass::$MyClasses as $class) { $class->MyMember = 0; } } }.

See Visibility and Scope Resolution Operator (::) in the oop5 chapter of the php manual.

Within static methods, you can't call variable using $this because static methods are called outside an "instance context".

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions