I've encountered with a situation in which I need to call __destruct
if a condition is true in __construct
!
how to call destructor in constructor?
is there a way for that?
I can do this with a trick, but I need to do it inside my class, the goal is to destruct a class if does not meet my criteria.
EDIT :
In constructor I check for user permissions, If a user who is trying for deleting a post is someone other than admin, then the object of the class should not be instantiated, or in other word should be destructed.
Avoid doing actual work in your constructors. Constructors should essentially build an instance of the object. It doesn't make sense to call __destruct()
from the constructor, because the instance is not owned by the method of the class, but by the function that called the new
operator.
Ask yourself what should be returned by the new
operator at the call site in the case the object can't be constructed?
$obj = new Class();
What should be assigned to $obj
if the object cannot be properly constructed? NULL
? Or perhaps the code doesn't make sense at all?
You have a few options:
Remove any non-construction code from the constructor, so that the constructor cannot fail at all. Put whatever code that fails in another method. Example:
$obj = new Class();
if ( $obj->initSomeStateThatMayFail() ) {
// good, proceed...
} else {
// handle failure somehow...
}
unset($obj); // calls __destruct()
If a failure in the constructor should be signaled to caller, use a static factory method that tests whether it is valid to instantiate the class, and returns either null
or the new instance appropriately.
class Class {
static function makeClass() {
if ( testIfValidToInstantiate() ) {
return new self();
} else {
return null;
}
}
// ...
}
$obj = Class::makeClass();
if ( $obj !== null ) {
// proceed...
}
If the code doesn't make sense at all if the class cannot be constructed, perhaps you should throw an exception.
class Class {
function __construct() {
if ( !testIfValidToInstantiate() )
throw new Exception( "Unexpected condition" );
// ...
}
}
try {
$obj = new Class();
// ...
} catch ( Exception $e ) {
// do something to handle this situation...
}
Please understand, that calling the destructor is not the same thing as removing the class from the working set!
If myClass is the class, in which you destruct in the constructor, then
$a=new myClass();
will still result in $a being assigned to an instance of myClass!
You can't destroy the object from the constructor, but you can have it set a variable, and it's true destroy it after you create it. Something like this:
<?php
class test{
function __construct(&$dest=false){
echo "Constructed\n";
if(1 == 1){ //Some condition
$dest = true;
}
}
function __destruct(){
echo "De-Constructed\n";
}
}
$a = new test($d);
if($d){
unset($a);
}
echo "End Prog\n";
Demo: http://ideone.com/fJxOq
If you throw an exception within the constructor, then the object will not be created... so if the constructor code identifies your condition, tidy up anything you need within your constructor and throw an exception... remembering to enclose your instantiation within a try/catch block.
class testClass {
public function __construct($killMe=false) {
if ($killMe) {
// Do any tidying up you need within the class
throw new Exception('KILLED');
}
}
}
try {
$a = new testClass(false);
} catch (Exception $e) {
echo 'DIED IN CONSTRUCTION',PHP_EOL;
}
var_dump($a);
try {
$b = new testClass(true);
} catch (Exception $e) {
echo 'DIED IN CONSTRUCTION',PHP_EOL;
}
var_dump($b);