How to access objects of a class within an anonymous method?

Asked

Viewed 574 times

1

I researched a lot on the Internet but I did not find in how to develop, I will be very grateful for who can take me that doubt. I would like to develop a php class where you can access your methods within an anonymous method as shown below:

MyClass::method('param1', function($class){
    $class->print('Hello World');
});

This example of usage I found in Laravel in its Mail class, example:

Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
    $m->from('[email protected]', 'Your Application');
    $m->to($user->email, $user->name)->subject('Your Reminder!');
});
  • Do you want to MyClass::method automatically turn an instance new MyClass and be passed on as a value to $class? What is the purpose of this? Object orientation is used to organize and when necessary, if it is simply by "beauty" that you are wanting to do this already soon I tell you that you are using badly to Object orientation

  • Hello, I edited my question to be more specific, in fact I found this example of use in the Laravel Mail class, I would like to take a question of how to develop a class that uses exactly this way to study even. Thank you

  • But that there is a purpose, you want to use something even without a prior? The $m returns an instance of message to interact with your controller, I mean it’s totally possible for you to do this, if you look at 5.5 they even changed the https://laravel.com/docs/5.5/mail... I mean it’s actually simple, But to do it by doing it is not enough to say that it is a great use for the use of OO. I’ll try to make an example here.

  • Purpose I even have, but I can not put them into practice without before understanding the concept and the best way to do, thanks for the tips is being of great help.

  • Just tell me two things: 1. you know the difference of an instance and a "static"? 2. You need that $class in function($class) be self instantiative for each time you use MyClass::method or you want it to create a unique instance?

  • In answer to question 1: yes, 2 yes.

  • Last question: the anonymous function must have access only to the public elements of the class or can access private methods?

  • Following the above logic I believe it makes more sense for him to access only the public methods of the class.

Show 3 more comments

1 answer

1


If the anonymous function only has access to public elements, whether attributes or class methods, the code is basically what Guilherme described in the comments: a static method that creates a new class instance and passes it as a parameter to the anonymous function. Simple as that. See an example:

class Groot {

    private function __construct() {
        $this->name = "I'm groot!";
    }

    public function getName() {
        return $this->name;
    }

    public static function create($callback) {

        // Cria uma nova instância da classe:
        $new = new self();

        // Invoca a função anônima passando a instância como parâmetro:
        $callback($new);

    }

}


Groot::create(function ($self) {
    echo $self->getName(), PHP_EOL;
});

See working on Ideone | Repl.it

In order for the anonymous function to have access to private elements, you will need to associate the function to the object through the Closure::bind. Take an example:

class Groot {

    private $name;

    private function __construct() {
        $this->name = "I'm groot!";
    }

    public function getName() {
        return $this->name;
    }

    public static function create($callback) {

        // Cria uma nova instância da classe:
        $new = new self();

        // Associa a função ao objeto:
        $callback = Closure::bind($callback, null, $new);

        // Invoca a função anônima passando a instância como parâmetro:
        $callback($new);

    }

}


Groot::create(function ($self) {
    echo $self->name, PHP_EOL;
});

See working on Ideone | Repl.it

Even $name being a private attribute, the anonymous function will have access to it.

  • Thank you so much for the tip friend, it helped me a lot, I will be studying more about this type of approach.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.