I'm learning OO stuff, and came across this:
class n{
private function f[$v]{
return $v*7;
}
function c[]{
return $this->f[5];
}
}
$o = new n;
echo $o->c[]; //returns 35
Doesn't that beat the purpose of declaring functions private
if I can access it still from outside the class? Shouldn't this be blocked altogether? Am I missing something? Please help clear up. Thanks
Ariful Islam
7,5357 gold badges34 silver badges54 bronze badges
asked Nov 15, 2011 at 8:49
1
Public functions are meant to perform operations on an instance of that class. Say, Save[]
.
The internal workings of Save[]
are not interesting for the caller; he simply wants to save it and doesn't care how that happens.
As a matter of style, you might or might not want to actually perform the saving in that method. It might depend on design choices, or on properties of the object. See:
class FooObject
{
private $_source;
public function Save[]
{
if [$this->_source == "textfile"]
{
$this->saveToTextfile[];
}
elseif [$this->_source == "database"]
{
$this->saveToDatabase[];
}
}
private function saveToTextfile[]
{
// Magic
}
private function saveToDatabase[]
{
// Magic
}
}
You don't want anyone to call the private methods directly, because they are for internal use only. However, a public method may indirectly call a private method.
answered Nov 15, 2011 at 8:55
CodeCasterCodeCaster
142k22 gold badges210 silver badges256 bronze badges
0
You missed the point, that you don't call a private
method from outside. You call a public
method [missing visibility modifier =
public
] from outside and from there you call a private
method from inside the class.
answered Nov 15, 2011 at 8:52
KingCrunchKingCrunch
126k20 gold badges147 silver badges171 bronze badges
Declaring things private
just hides them from the public interface. You are perfectly entitled to do things like in your code snippet; i.e. implement the public interface in terms of the "hidden" private internal methods.
If you were writing a library, you would probably leave your public interface well-defined, and try to minimise changes between releases [so that users don't have to maintain their code]. But you would be free to modify internal stuff any way you like, without any cost to the user.
answered Nov 15, 2011 at 8:51
You can't call your function even from your code, you call
$this->f[5];
via a call to
c[]
You wouldn't be able to call f[] with a different parameter, at least in the code you posted.
But no, calling private methods from public ones isn't a code smell.
I mean, where else would you expect them to be called from if not from public methods?
answered Nov 15, 2011 at 8:53
Luchian GrigoreLuchian Grigore
248k63 gold badges447 silver badges613 bronze badges
The private
method means that it does internal stuff that does not concern
the external world [outside the class].
answered Nov 15, 2011 at 8:54
samurasamura
4,3651 gold badge17 silver badges26 bronze badges
You cannot call $o->f[]
directly from outside, which is all private
is supposed to protect. If you couldn't call a private
method if any public
method is involved anywhere in the call stack, you couldn't call it at all. Some method of your class needs to be called from outside; and may then make calls internally.
answered Nov 15, 2011 at 8:55
deceze♦deceze
497k81 gold badges719 silver badges867 bronze badges
Your code is correct, however if you don't specify the method permission [ie: public, private or protected] the default behaviour of public be assigned to it. So your function c[] is actually public at this point which is why you can call it from outside of the class.
If you switch it to private or protected you will then receive the error you are expecting.
answered Nov 15, 2011 at 8:58
Joseph WoodwardJoseph Woodward
9,1115 gold badges43 silver badges62 bronze badges
answered Oct 28, 2017 at 11:48
If your class is not the only one [which contain a main[] method called by the startup mechanism], there's must be an outside-of-the class caller somewhere in tha calling "chain", otherwise its methods should be never called.
answered Nov 15, 2011 at 9:00
ern0ern0
3,11024 silver badges35 bronze badges
1