NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Private variables


Joined: 20 Sep 2006
Posts: 59
Reply with quote
Here is the code (php5):
Code:

class base
{
   private $x;
   
   function __construct()
   {
      $this->x = 1;
   }
}

class woot extends base
{
   function test()
   {
      // break here:
      print $this->x;
   }   
}

$w = new woot();
$w->test();

Put a break on the "print $this->x;" line.

When you hover over $this the tooltip is 'x=1', when you hover over the "->x" the tooltip is "$this->x = undefined"

In the imediate window:

print $this->x;
: Error Undefined property: woot::$x

In the 'locals' window click on the "+" next to this, it shows that $this->x == 1

I'm not sure what should happen in this situation, but if the tooltip had "You are trying to access a private member" would be helpful.

If all the windows showed the same thing it'd be better.

I find this error a lot when exploring PEAR and other peoples code.

Cheers,

monk.e.boy
View user's profileFind all posts by monkiesSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8342
Reply with quote
Quote:
I'm not sure what should happen in this situation,

I'd recommend you to check php manual http://www.php.net/private
Property x in woot class is NOT defined.

Quote:
the tooltip is "$this->x = undefined"

it's correct.

Quote:
In the imediate window:

print $this->x;
: Error Undefined property: woot::$x

it is correct too.

Quote:
but if the tooltip had "You are trying to access a private member" would be helpful

but it is NOT private member. It is UNDEFINED member Smile, or not a member at all.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 20 Sep 2006
Posts: 59
Reply with quote
So the other points in my post, where the IDE shows $this->x == 1 is wrong? The tooltip and the 'locals' window?

monk.e.boy
View user's profileFind all posts by monkiesSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8342
Reply with quote
Even though debugger showed you that property in the Locals, still it is not accessible from an instance of woot class.
PLEASE check the sample shown in the php manual. It is almost the same as yours and private property gives "not defined" too. It's intended behaviour of php. Nothing else.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 20 Sep 2006
Posts: 59
Reply with quote
I understand how private variables work.

I don't understand why the IDE shows $this->x as 1 and not undefined. We both agree it should be undefined. Why is the IDE telling me it has value '1'?

monk.e.boy
View user's profileFind all posts by monkiesSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8342
Reply with quote
It's because this value is in the parent class and debugger sees it. Try to assign $this->x with a value and you'll see _new_ x property created and debugger will show you two properties with the same name x. Debugger just shows you how php works internally.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 20 Sep 2006
Posts: 59
Reply with quote
> Debugger just shows you how php works internally.

Oh, OK. That makes sense.

But from my perspective as a programmer the tooltips, local window and imediate window seem inconsistant. Some show undefined, some show '1'. I think they should all show undefined.

monk.e.boy
View user's profileFind all posts by monkiesSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8342
Reply with quote
It's possible. But if we follow your suggestion, it will confuse too. Indeed, same instance will be shown differently in different contexts. While you're in the parent methods, you'll see private properties but as soon as you go out of those methods, private properties will _disappear_. Same goes to the protected properties. They are not accessible out of the class or its successors' scopes.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 20 Sep 2006
Posts: 59
Reply with quote
dmitri wrote:
It's possible. But if we follow your suggestion, it will confuse too. Indeed, same instance will be shown differently in different contexts. While you're in the parent methods, you'll see private properties but as soon as you go out of those methods, private properties will _disappear_. Same goes to the protected properties. They are not accessible out of the class or its successors' scopes.


That is how other IDEs work. Exactly what you said.

They also show the scope of the property, so you may be in 'base' and see "(private) $this->x = 1" and when you pop out into 'woot' that property would dissapear. This is what I would expect to happen Smile

I don't think this would confuse people. After all, this is what happens in your head while you are coding.

monk.e.boy
View user's profileFind all posts by monkiesSend private message
Private variables
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic