note 108991 deleted from language.oop5.basic by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 Jun 2012 15:28:43 +0000 |
| Subject: | note 108991 deleted from language.oop5.basic by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188579@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
there are time we would like an object to self destruct if the initializing argument provided during
object creation, dictate the object should exist at all.
<?php
class TestClass
{
private $have_girlfren=false;
//
private function __construct( $have_girlfren )
{
echo "sixth day<br>
";
$this->have_girlfren=$have_girlfren;
$this->existenceRequirement();
}
//
private function existenceRequirement()
{
if( $have_girlfren==false )
{
echo 'i want to die<br>
';
$this->__destruct();
throw new Exception();
}
}
///////////////////////
function __destruct()
{
echo "i am dead<br>
";
}
//////////////////
static function getNewInstance( $have_girlfren)
{
try{
$new_obj=new TestClass( $have_girlfren);
return $new_obj;
}
catch(Exception $e)
{
unset($new_obj);
echo "rest in peace<br>
";
return false;
}
}
//
function gotGirlFren()
{
return $this->have_girlfren;
}
}
$test_obj=TestClass::getNewInstance( false );
if( $test_obj )
echo "bye test oby closing<br>
";
else
echo "instance commit suicide<br>
";
echo "--------------<br>
";
echo "-the end--";
?>