Skip to content

Instantly share code, notes, and snippets.

@ssi-anik
Created May 29, 2020 14:41
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 ssi-anik/c8e98d2818227cac3f5fe6eb3409de80 to your computer and use it in GitHub Desktop.
Save ssi-anik/c8e98d2818227cac3f5fe6eb3409de80 to your computer and use it in GitHub Desktop.
PHP object Lifecycle.
<?php
echo 'script 1 start' . PHP_EOL;
class A12
{
public $script = null;
public function __construct ($script = 's1') {
$this->script = $script;
}
public function __destruct () {
echo 'destructing a12 - ' . $this->script . PHP_EOL;
}
}
class B12
{
public $script = null;
public function __construct ($script = 's1') {
$this->script = $script;
}
public function __destruct () {
echo 'destructing b12 - ' . $this->script . PHP_EOL;
}
}
function get_a12 ($s = 's1') {
return new A12($s);
}
function get_b12 ($s = 's1') {
return new B12($s);
}
$b12 = new B12('s1');
echo 'script 1 end' . PHP_EOL;
// script 1 ends
// script 2 starts
// <?php
// uncomment the following line when using multiple scripts
// include 'script1.php';
echo 'script 2 start' . PHP_EOL;
$a12 = new A12('s2');
echo 'script 2 end' . PHP_EOL;
// script 2 ends
// script 3 starts
// <?php
// uncomment the following line if using multiple scripts
// include 'script2.php';
echo 'script 3 start' . PHP_EOL;
get_a12('s3');
// comment any of the following adjucent lines, and uncomment the another one
get_b12('s3');
// $b2 = get_b12('s3');
echo 'script 3 end' . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment