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.
Do you want to
MyClass::method
automatically turn an instancenew 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– Guilherme Nascimento
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
– Rafael Andrade
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.– Guilherme Nascimento
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.
– Rafael Andrade
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 useMyClass::method
or you want it to create a unique instance?– Guilherme Nascimento
In answer to question 1: yes, 2 yes.
– Rafael Andrade
Last question: the anonymous function must have access only to the public elements of the class or can access private methods?
– Woss
Following the above logic I believe it makes more sense for him to access only the public methods of the class.
– Rafael Andrade