Skip to content

Instantly share code, notes, and snippets.

@mcloide
Last active August 29, 2015 14:17
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 mcloide/e6b42476e34ba225d7e6 to your computer and use it in GitHub Desktop.
Save mcloide/e6b42476e34ba225d7e6 to your computer and use it in GitHub Desktop.
Do you understand PHP garbage collection? What is the result of each of the gist files?
<?php
class foo
{
public $bar = 1;
function bar()
{
echo $this->bar;
}
}
class faz
{
public $baz = 2;
function bar()
{
echo $this->baz;
}
}
$foo = new foo();
$faz = new faz();
$bar = &$foo;
$baz = $faz;
$bar->bar = 4;
$baz->baz = 5;
$bar = $baz;
$baz = null;
$faz->bar();
$foo->bar();
<?php
class foo
{
public $bar = 1;
function bar()
{
echo $this->bar;
}
}
class faz
{
public $baz = 2;
function bar()
{
echo $this->baz;
}
}
$foo = new foo();
$faz = new faz();
$bar = &$foo;
$baz = $faz;
$bar->bar = 4;
$baz->baz = 5;
$bar = null;
$baz = null;
$faz->bar();
$foo->bar();
If you answered wrongly any of the results above, you really should read this:
http://php.net/manual/en/language.references.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment