Accessing Private Function from Outside Class
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()...
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
9 Answers
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.
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.
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.
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?
The private method means that it does internal stuff that does not concern the external world (outside the class).
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.
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.
<?php
//Accessing private method in php with parameter
class Foo {
private function validateCardNumber($number)
{
echo $number;
}
}
$method = new ReflectionMethod('Foo', 'validateCardNumber');
$method->setAccessible(true);
echo $method->invoke(new Foo(), '1234-1234-1234');
?>
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.