Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem calling class from the ProcessCallbackTask anonymous method #24

Open
gubi opened this issue Jun 15, 2018 · 1 comment
Open

Problem calling class from the ProcessCallbackTask anonymous method #24

gubi opened this issue Jun 15, 2018 · 1 comment

Comments

@gubi
Copy link

gubi commented Jun 15, 2018

I have some errors with this piece of code:

class A {
    public function run($machine_name) {
        echo $machine_name;
    }
}


class B extends A {
    public static function listen() {
        $manager = new ProcessManager();
        $A = new A();
        foreach(self::get_devices() as $k => $machine) {
	    $task[$k] = new ProcessCallbackTask(function() use($k, $machine, $A) {
                A->run($machine->name); // No way
                $A->run($machine->name); // No way
            });
            $manager->addTask($task[$k]);
        }
	while($manager->tick()) {
	    usleep(250);
	}
    }
}

How to call A class method from ProcessCallbackTask in B class?

I would like to process async commands on a set of online machines, taken from db
Thanks in advance

@TheTechsTech
Copy link

$A = new A();
 foreach(self::get_devices() as $k => $machine) {
   $task[$k] = new ProcessCallbackTask(function() use($k, $machine, $A) {
   A->run($machine->name); // No way
   $A->run($machine->name); // No way
});

Just to let you know, there are problems with your code sample.

  1. There is no need for $A = new A(); you have extended the class, the methods is now part of class B.
  2. Since the methods of A are included/part of B, you need to reference like $this->run().
  3. If you want to access class A directly you need to do A::run(); not A->run($machine->name);

I don't see how you code would run right under any conditions without those changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants