473,219 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

Why is access and visibility mixed up in PHP?

Well, as is customary when you're new to a group, you tend to post quite a
bit, :) so here's one more. Some things I've been wondering about with PHP
(5).

Today, I worked on an implementation of a finite state machine. Unlike the
pear::fsm, this one supports hierarchical states, and I intend to make it
available, as well. It exists in both PHP 4 and 5 versions, but only the
PHP5 version is relevant for this example. 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 (I only
quote the bare minimum needed for illustration):

class fsm
{
...
public function process($signal)
{
// null_action() is called from here (unless another action function is
specified for a transition)
}

private function null_action($from_state,$to_state)
{
}
}

class my_fsm extends fsm
{
private function null_action($from_state,$to_state)
{
echo "Transition from $from_state to $to_state";
}
}

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?

Regards,

Terje
Jul 17 '05 #1
2 1469
["Followup-To:" header set to comp.lang.php.]
On 2005-01-22, Terje Slettebø <ts*******@hotmail.com> wrote:

[snip code]
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.
Actually, you can have the constraint. But it acts as expected in PHP
and not the way you would like it to act.
Why is it done like this?


I think you can better ask this on a php-internals ml.
My guess is that this is done because OOP in PHP5 seems to have been inspired by Java.
(Would also explain why we don't have friend visibility)
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #2
"Tim Van Wassenhove" <ti***@users.sourceforge.net> wrote in message
news:35*************@individual.net...
["Followup-To:" header set to comp.lang.php.]
On 2005-01-22, Terje Slettebø <ts*******@hotmail.com> wrote:
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.
Actually, you can have the constraint.


No. The constraint was that it should be possible to override the function
in a derived class, but not to call it, there. Anyway, from my experience
with the FSM, it seems PHP behaves sufficiently different from C++ (and
Java, actually) in this context, that the issue is kind of moot. To
illustrate:

class base
{
public function g()
{
$this->f(); // Succeeds!
}
}

class derived extends base
{
protected function f() { echo "f()"; }
}

$object=new derived();

$object->g(); // Prints "f()"

IOW, it's possible to call a derived class function from the base class,
even if it's not defined in the base class (this is rather unlike Java, but
actually very useful for the FSM :) ), as long as the actual object being
used is of class derived.
But it acts as expected in PHP and not the way you would like it to act.


It seems that "what is expected" may be a tad subjective. :) For a C++
programmer, the way I described (being able to override, but not call, a
private virtual function) makes complete sense, at least to those that know
that access (being able to call) and inheritance/visibility (being able to
override) are orthogonal in C++.

For those interested, here's an article about it (C++):
http://www.gotw.ca/publications/mill18.htm However, the advice to prefer
public nonvirtual functions, delegating to private virtual functions, is not
something that is generally agreed upon (but also has nothing to do with my
example, as it doesn't do this).

However, I guess the Java/PHP way of doing it is reasonable, as well.

Regards,

Terje
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
2
by: Nasir Wasim | last post by:
How can Access DIV Properties in Script while using Netscape 7.2. I want to use the div id with it's Visibility and left propertyin script but it's not working :-( Main File : ----------- ...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
0
by: TheCoder | last post by:
I am making a D-base with web conectivity for my class project. I have everything working but the subit button sends the data to the correct fields but afterwards it wants to reproduce new blank...
1
by: Thomas Albrecht | last post by:
My application fails during initialization of the dlls with an ExecutionEngineException and a access violation in the MFC app. The structure of the program looks like: MFC app -> mixed DLL ->...
5
by: XFER | last post by:
Does anyone know how well 10 concurrent users will perform on the above config? Are there any known issues, limits to using MS Access with IIS 5 and ASP.net on a non- ..net server (NT)? thanks.
3
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
7
by: Ashutosh Bhawasinka | last post by:
Hi, I have a C# .Net application which needs to use some feature which can be only developed in Visual C++ (its extended MAPI). The C# exe will be supplied to users without a setup. What kind...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.