Skip to content

Instantly share code, notes, and snippets.

@tentacode
Last active May 28, 2018 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tentacode/8627566bb59cbed5df9bb0c208bf1984 to your computer and use it in GitHub Desktop.
Save tentacode/8627566bb59cbed5df9bb0c208bf1984 to your computer and use it in GitHub Desktop.
<?php
class Something
{
public function __construct()
{
print "J'ai été instanciée.".PHP_EOL;
}
public function __destruct()
{
print "Chuis morte.".PHP_EOL;
}
public function __invoke()
{
print "J'ai été invoquée.".PHP_EOL;
}
}
(new Something())();
// équivaut à
$something = new Something();
$something();
// aussi équivaut à
$something2 = new Something();
$something2->__invoke();
// aussi équivaut à
$something3 = new Something();
call_user_func($something3);
@tentacode
Copy link
Author

tentacode commented May 28, 2018

Le truc rigolo c'est que la seule de différence entre les 4 méthodes est que la première appelle le destructeur directement après son instanciation (normal quand on y pense), ce qui affichera :

J'ai été instanciée.
J'ai été invoquée.
Chuis morte.
J'ai été instanciée.
J'ai été invoquée.
J'ai été instanciée.
J'ai été invoquée.
J'ai été instanciée.
J'ai été invoquée.
Chuis morte.
Chuis morte.
Chuis morte.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment