Reputation: 985
I have a Bcrypt.class
which contains my hashing methods and I have a userDAO.class
which contains my register method.
In my Bcrypt.class
the methods are private. How can I access them in my userDAO.class
so I can hash my password?
Please let me know if you need to see any code.
Upvotes: 3
Views: 624
Reputation: 2656
As of PHP 5.4 and ReflectionMethod::getClosure() it looks like accessing private methods from the outside is possible. To be honest - had to try out the following solution before believing it actually works.
Code from the User Contributed Notes section by Denis Doronin.
function call_private_method($object, $method, $args = array())
{
$reflection = new ReflectionClass(get_class($object));
$closure = $reflection->getMethod($method)->getClosure($object);
return call_user_func_array($closure, $args);
}
class Example
{
private $x = 1;
private $y = 10;
private function sum()
{
print $this->x + $this->y;
}
}
call_private_method(new Example(), 'sum');
// Output is 11.
Upvotes: 1
Reputation: 158280
You cannot access private
methods from outside of the class declaring them. If API developer decided to use private
then there is no chance. I personally prefer protected
in almost every situation. Some API developers don't..
If you have personal access to the source code of the Bcrypt
class and you can change it without breaking anything then make the methods either protected
and extend the class or make them even public
Another design approach would be to place the algorihms in a separate class and use them in the Bcrypt
class and others
Upvotes: 1
Reputation: 68556
Make your methods in the Bcrypt
class either public
or protected
.
These are things you should keep in mind.
Public Methods
: Can be accessed from anywhere Protected Methods
: Can be accessed by the class and other classes that inherits it.Private Methods
: Only the corresponding will have access.Upvotes: 1