This is the thing... I have a simple class with some functions but I need to change the way that I call those functions.
Originally the class look like this
<?php
class Bcrypt {
const DEFAULT_WORK_FACTOR = 8;
public static function hash($password, $work_factor = 0) { ... }
public static function check($password, $stored_hash, $legacy_handler = NULL) { ... }
}?>
Now I need change the way that model call this class
Originally the model look like this
Bcrypt::hash($data['password'])
What I need to do is change this call to be like this:
$this->bcrypt->hash($data['password'])
I know maybe this is a simple question but I need to clarify some concepts...
Make your functions non-static.
And do some reading on classes and objects. In order to use ->, you will have to instantiate your class.
The syntax for the function call is determined by the function being static
or not, so the direct answer is "remove the static
keyword from the function declaration".
However, a method being static
or not should never be a matter of preference; it should be a design decision. You don't say why you need to make this change, there is nothing in the posted code that provides a relevant hint, and in this case it seems perfectly OK for the methods to be static
in the first place.
So that leaves open the question: why do you ask?
There are 3 ways in which you can call a method or a variable.
Static variables/methods from outside a class
class Test {
public static function testFunc() {}
}
Test::testFunc();
Static variables/methods from inside a class
class Test {
public static function testFunc() {}
public static function testFromInside() {
return self::testFunc(); // you can do this with Test::testFunc() as well
}
}
Test::testFromInside();
Non static variables from inside the class
class Test {
public $test;
public function testFunc() {
return $this->test;
}
}
$test = new Test;
$test->test;
$test->testFunc();
If it's unclear, let me know and I'll try to explain better.
It's sounds you don't know the difference between public and static.
Static means that you do not need an instance to call a function like you do.
If you want to use the arrows, you need to make your functions public and make an instance first.
What you must do:
//create instance of the class first
$bcrypt = new Bcrypt();
//call the instance variable and choose your function
$bcrypt->hash($data['password']);
It is very important that you know the difference between static and public. I hope it helps you!