NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
private vars magic getter/setter and code complete


Joined: 07 Dec 2009
Posts: 10
Location: Remote consultant
Reply with quote
Forgive me if this has been beaten to death already. I looked for references to this but couldn't find a solution. There was one thread on a similar track but no answer.
I like to build all my classes with private vars and public getter/setter functions. I'm now using the magic functions __get and __set to process property access. Code complete doesn't seem to be able to see the private variables. Here's a sample class

<?PHP
// Animal.php

Class Animal {
private $legs;

function __construct(){
$this->legs = 2; // default
}

public function __get($key) {
if (property_exists(get_class($this), $key)) {
$method = 'get' . ucfirst($key);
return $this->$method();
}
else
throw new Exception('Property ' .$key . ' not found on object '.__CLASS__.'<br />');
}
public function __set($key) {
if (property_exists(get_class($this), $key)) {
$method = 'set' . ucfirst($key);
return $this->$method();
}
else
throw new Exception('Property ' .$key . ' not found on object '.__CLASS__.'<br />');
}

private function setLegs($value) {
if (isset($value) && is_integer($value) && $value > 0 && $value <= 100) {
$this->legs = $value;
else
throw new Exception('Invalid data '. $value .' in setLegs');
}
private function getLegs() {
if (isset($value))
return $this->legs;
else
throw new Exception('Attempt to read data before being set in getLegs');
}
}
?>

<?php
// TEST.PHP

require_once('Animal.php');

$myMammal = new Animal();
$myMammal-> // code hint comes up with __construct, __get & __set only

Anyone know if phpEd can see the private variables somehow?

Thanks
/MB
View user's profileFind all posts by mbakerSend private messageVisit poster's website


Joined: 29 Jun 2009
Posts: 26
Reply with quote
No, and AFAIK there is no way for PhpEd to know that at all. Code completion can only complete on variables it can see. In your case, the only variable that PhpEd would be able to complete would be the $legs, but only in the scope of its own class because it is private. Since it is not public, it wont see anything on derived objects from that class.

As for the __get and __set, there is no way for PhpEd to know your intentions to use that for mapping to private vars. You can actually supply a property value to a class that utilizes a __set function that has no mention of the property anywhere in the code and yet still has value because the __set function could have logic inside of itself to handle the setting of that property on other areas of the class.

So because of that, AFAIK there is no programmable way to have PhpEd to uncover your intentions in using __set and __get to be able complete your code automatically.

Have a great day!
View user's profileFind all posts by DracosSend private message


Joined: 07 Dec 2009
Posts: 10
Location: Remote consultant
Reply with quote
We could implement a function in the class GetClassVars... PhpEd could call that? Would that be a worthwhile ECR? Too many variables to consider?

public function GetClassVars(){
return get_class_vars(get_class($this));
}

/MB
View user's profileFind all posts by mbakerSend private messageVisit poster's website


Joined: 29 Jun 2009
Posts: 26
Reply with quote
Well, heres the way I see that. Its opening up a can of worms. What you are asking them to do is create a special method that is only used by PhpEd and not recognized anywhere else. If they implement that, then I can see a whole slew of requests coming in that will ask for special method this, special property that, and so on. Then no only would have all those requests to implement, but for each one you do implement you would have to maintain and verify non-conflict across future PHP and PhpEd updates. I dont think that would be something we would want as more attention would be taken for that, instead of continuing development of PhpEd to stay on top of PHP releases.

Also, if I am not mistaken, PHP is planning on implementing standard getter setter functions for properties (i.e. public function set thisProperty()) which then would provide the structure for code completion to work. Dont quote me on that, but I believe they are going to. I think I read that somewhere, in some blog, referencing some magazine, in an article read by some one at a rest stop. Quite viable, trust me. Smile
View user's profileFind all posts by DracosSend private message


Joined: 07 Dec 2009
Posts: 10
Location: Remote consultant
Reply with quote
I figured it would be a can of worms as you suggest and yes - everyone would have their own favorite feature they want to have added. One could say that's how software is improved - p)
Of course I'm joking. If PHP is adding real live getter setter syntax then I can deal with this until then.

Thanks for the info
/MB
View user's profileFind all posts by mbakerSend private messageVisit poster's website


Joined: 07 Dec 2009
Posts: 10
Location: Remote consultant
Reply with quote
I was just exploring some more PHPDoc options and code hinting... I see something I had not spotted before

/**
* @package ThePack
* @author foo
*
* @property $legs
*/

Class Animal {
private $legs;

... balance of class like it was before
}

I notice when I include the @property tag for an item in the class then code hints will come up for that even though the item is declared as private within the class. Should this work OK or will the declaration of the @property interfere with the operation of the __get __set functions I have set up?
I can try this out and let you know if you don't know already.

/MB
View user's profileFind all posts by mbakerSend private messageVisit poster's website


Joined: 07 Dec 2009
Posts: 10
Location: Remote consultant
Reply with quote
I'll go ahead and answer my own question here... yes it does work and it does not appear to interfere with the usage of the __get and __set functions. The @property is discovered above the class definition and it shows up in the code hints but during operation it still goes through the __get and __set functions so you can do whatever property filtering or data validation is required.

Hope this helps the next one searching for this

/MB
View user's profileFind all posts by mbakerSend private messageVisit poster's website
private vars magic getter/setter and code complete
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