Hello Dan,

Friday, October 10, 2003, 10:53:20 AM, you wrote:

> Marcus-

> Marcus Börger wrote:

>> Hello Dan,
>>  
>>
>>> Currently, I don't see an easy way of accessing the constants of a child
>>> class from a base class.
>>>   
>>
>> Use the child's class name:
>> php -r 'class t{static function f(){echo tt::c;}}class tt extends 
>> t{const c="Hello\n";} t::f();'
>>
>>  
>>
> Yes, this is possible, but it assumes you always only ever have one 
> child class, which
> the base class knows the name of (unlikely). You could figure things out 
> dynamically,
> as I provided some example code for, but this would become unwieldy with 
> 10s to
> 100s of derived classes.

>> But i guess you want something dynamically as $class::const, right?
>>
>>  
>>
> right.

>> the next example uses 'self' which doesn't work because self is bound at
>> compile time:
>> php -r 'class t{function t(){echo self::c;}}class tt extends t{const 
>> c= "Hello\n";} $o=new tt;'
>>
>> So you'd need something like '$this::c' which is impossible right now.
>>
>> Funny thing is the following which returns NULL what makes absolute no sense
>> to me. It somehow looks like either the correct constant is used but it is
>> uninitialized or there is an error missing.
>> php -r 'class t{function t(){var_dump($this->c);}}class tt extends 
>> t{const c= "Hello\n";} $o=new tt;'
>>
>>  
>>
> I believe this is correct behaviour. When instantiating an object the methods, 
> properties,
> etc. of the child class and any and all parents are in essence aggregated together 
> into a
> new object (the instance). It would make sense (at least to me) that 'class 
> constants'
> should NOT be aggregated together on instantiation. They should be constant only for
> their particular class definition and all code provided by that class.

> So you could have a const foo = 'bar' in both a parent and child class, and they 
> would
> be 2 unique constants only accessable by code inside their class definition (how it 
> works
> right now).
> I take this position because, from a programmer standpoint, when i type 'const foo',
> I would like to believe I'm defining a *constant* name/value pair that could not be
> changed at runtime. If constants are aggregated together between all parent/child 
> class
> definitions on instantiation, then there could be the potential for conflicts and 
> overriding of
> constant values.

> But I digress.. I just would like an easy way to get to child constants. Having to 
> use
> something like myClass::constant from *inside* an object instance *of* myClass seems
> kind of... strange.

Constants are bound to the class rather then to the objects. Hence they
behave pretty much like static properties or default values of declared
properties and like the latter they are read only.

The only question here is whether we want to be able to access static
and/or const class members through something dynamically like $this at
runtime. Everything else is perfectly correct in place.

And of course constants are public. So perhaps you might want to be able
to apply visibility to constants, too? If so i must dissappoint you with
the fact that it is currently impossible to do that and the amount of work
to enable this seems so high that it is unlike to happen.

-- 
Best regards,
 Marcus                            mailto:[EMAIL PROTECTED]

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to