-5

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...

Camilo Salas
  • 143
  • 2
  • 10

4 Answers4

3

Make your functions non-static.

And do some reading on classes and objects. In order to use ->, you will have to instantiate your class.

SBI
  • 2,322
  • 1
  • 17
  • 17
  • I just try that but I get: Fatal error: Call to a member function hash() on a non-object in – Camilo Salas Jan 17 '13 at 13:43
  • That's the instantiation part. You need to create an object on which to call your methods first. This is absolutely basic OO-programming, so you should at least read up a bit on this. $bcrypt = new Bcrypt(); – SBI Jan 17 '13 at 13:54
1

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!

woodle
  • 59
  • 5
0

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?

Jon
  • 428,835
  • 81
  • 738
  • 806
0

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.

Vlad Preda
  • 9,780
  • 7
  • 36
  • 63
  • Ok.. I read about but.. I'm a little frustated.. Some people seems to be born knowing this kind of things but I dont.. coul you give me more explicit guide to do this?? – Camilo Salas Jan 17 '13 at 14:35