Re: Why is access and visibility mixed up in PHP?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Terje Slettebø <tslettebo@xxxxxxxxxxxx>:
> (I've posted this to the PHP newsgroups, as well, but as many here might not
> read them, I post here, as well. I hope that's not considered "overboard",
> and if so, please let me know)

The newsgroups are simply an NNTP interface to the mailing lists -- use
one or the other; either way, it gets to the same place.

> Today, I worked on an implementation of a finite state machine. 
<snip>
> The base class defines some
> (virtual) functions that may be overridden in a derived class, but they are
> only called from the base class. My original code was as follows
<snip>:
>
<snip -- class with private method, subclass with private method>
>
> In C++ this would work just fine: As null_action() is called from fsm, and
> it's private in fsm, this works fine. It gets overridden in my_fsm, but
> being private in fsm, it can only be called there (not in my_fsm), which is
> as intended. This is because access and visibility are orthogonal concepts
> in C++: The access specifiers only specify who are allowed to access (as in
> calling, taking the address of, etc.) a function, but it doesn't affect
> overriding.
>
> The reason for this is as follows (from "The Design and Evolution of C++"):
> By not letting the access specifiers affect visibility (including
> overriding), changing the access specifiers of functions won't affect the
> program semantics.
>
> However, this is not so for PHP...
>
> The above won't work, or at least not work as intended: The function
> null_action() will only be visible in the class it's defined, and therefore
> the derived class version won't override the base class version. In order to
> get it to work, the access specifiers have to be changed to protected. This
> means that derived classes may also _call_ the function, something that is
> not desired. This means I can't enforce this design constraint of having
> this function private.
>
> Why is it done like this?

I'm not sure why the behaviour is as it is, but I do know that PHP
developers were heavily influenced by Java when writing the new PHP5
object model; I suspect your answers may lie there. 

One question I have to ask of you: why would you want the derived class
to be able to know a method exists if it will not be able to call it?
This seems to me to be... well, silly. Either the method is available to
the class or it isn't and/or the method is available to an instantiated
object or it isn't; visibility as being orthagonal to access just
doesn't make a lot of sense to me. Perhaps you could make a case as to
when this would be beneficial?

You might also want to take some of your questions to the php-dev list
-- they seem to be more related to the internals of PHP than PHP usage.

-- 
Matthew Weier O'Phinney           | mailto:matthew@xxxxxxxxxx
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux