473,386 Members | 1,810 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,386 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 1474
["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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.