1

I've been seeing multiple question regarding similar problem but, I can't recall seeing one regarding mine, as I couldn't really "implement" the other solution to get my problem solved, so I'm asking you guys, how to correctly pass a class method to a function in the __construct, or if if it's even possible to pass one before class has initiated.

So, when I initiate the class A i have some config values to be increased, in order to have them set and available.

function __construct(){
    require 'configs/production.php';

    function increase(&$value,$key){
        $value += DB::$speed * 0.05 * $value;
    }

    array_walk($production,'increase');

    $this->production = $production;
}

Ok so this is working, but, I'd like to make the define the increase as a public function in order to have it available for future use. So using this, will give me an error no matter how I pass the function.

function __construct(){
    require 'configs/production.php';

    array_walk($production,callable 'increase'); // gives error
    array_walk($production,$this 'increase'); // gives error too
    array_walk($production,A 'increase'); // gives error again
    array_walk($production,$this->increase); // is of course undefined
    array_walk($production,$this-> increase()); // of course, lacking paramas

    $this->production = $production;
}

public function increase(&$value,$key){
    $value += DB::$speed * 0.05 * $value;
}

Remember, I'm inside class A. And I'm trying to use type hinting, though, OtherClass $var should work, how am I supposed to send a method from same object.

PHP Version on dev/production: same, 5.5.11

2 Answers 2

1

To pass a class method to a function, you use array(object, methodname):

array_walk($production, array($this, 'increase'));

For a static method, you use array(classname, methodname).

Sign up to request clarification or add additional context in comments.

Comments

1

First, you are falling into the common trap of thinking that PHP allows nested functions; it doesn't, it just lets you define global functions at any time. Your first constructor will error if you call it twice, because it defines the global function increase whenever it is run.

Second, you need the right syntax for passing a method as a callback, which is array($object, $method_name). So in your case, array_walk($production, array($this, 'increase')); or using the short array syntax, array_walk($production, [$this, 'increase']);. The PHP manual has a page explaining this, with examples.

4 Comments

i'm not falling into the trap you're speaking about. I'm well aware I shouldn't use that function into the constructor but, why do you think I've asked here how to remove that from there if I wasn't aware of it ? Regarding the multiple instance on that file, don't worry, I'm always making sure I use one intance per session.
@edduvs The point is it's not really in there at all - since it's global, and can only be defined once, it might as well be completely separate at the top of the file. A lot of people get confused about that, but if you're not, great. :)
Also, you said you were moving the function "in order to have it available for future use", which sounded like you thought the current function was in some way private to the constructor where it's defined.
yes, I knew you'll say that while I was previously commenting your answer, the main purpose of my question was to make it a method, though I knew it's not OK to have it there, but having accomplished the main purpose, solves the secondary problem like that globally scoped function too. Anyway, thanks for the heads up, you couldn't knew I was already aware of that, thats why I'll UP you too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.