Re: do_declare_property()
| From: | Teodor Cimpoesu | Date: | Wed, 12 Jul 2000 16:55:16 +0000 |
| Subject: | Re: do_declare_property() | ||
| References: | 1 2 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-24345@lists.php.net to get a copy of this message | ||
Hi John!
On Wed, 12 Jul 2000, John Keiser wrote:
> > Of course it will know it's a class, and when parsing the class def.
> > the rewrite takes place.
> >
> > Of course, we don't really need to have `$this->__bar', I though we
> > can also have
private var $bar', and rewrite the member to
> > _Class__bar'
> > to ensure it's `privateness'.
> >
>
> Simple question. Couldn't you have a normal public variable that happened
> to start with two underscores? Isn't there a $ or something that cannot be
> used in an ordinary variable that could be used for this internal marker?
>
> What would happen in the following example:
>
> class test {
> var __yoyoman;
> private var yoyoman;
> }
>
> Or even simpler, what if just var __yoyoman was declared? Would the access
> fail even though it was public?
Depends on the implementation of the private.
I proposed it the Python way: rewrite the variable name to something else,
and when you encounter an access request, go get the actual attribute:
i.e.
class Foo {
var $__myprivate;
function getIt() { return $this->__myprivate;}
}
what should happen? when the class is parsed, don't add `__myprivate' as
an attribute, but add instead _Foo__myprivate.
Then apply a simple rule when the user wants `__myprivate':
is the access within a class method? yes - get _Foo__myprivate, else we have
an access violation.
e.g.
$this->__myprivate is actually translated into something like
$this->_Foo__myprivate.
you need the class name to reconstruct the real attribute, and you will always
have it when within a method (I guess :)
Same goes with `private' keyword, but there is some glitch here.
Andi pointed you would need to know the class at compile time, so to check
if $this->yoyoman' is private or not, but with this __' hack you just look
at the variable name: does it start with 2 x _ ? deny access if not in a method scope.
-- teodor