__destruct在__construct? [英] __destruct in __construct?

查看:168
本文介绍了__destruct在__construct?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果条件在 __结构中为真,则我需要调用 __ destruct 的情况。



如何在构造函数中调用析构函数?



b
$ b

我可以用一个技巧,但我需要在我的类里面做,目标是如果不符合我的标准,破坏一个类。



EDIT:



在构造函数中,我检查用户权限,如果尝试删除帖子的用户

解决方案

避免使用管理员之外的其他人,而不应该实例化该类的对象,否则应该被破坏。在你的构造函数中做实际工作。构造函数应该基本上构建对象的实例。从构造函数中调用 __ destruct()没有意义,因为实例不是由类的方法拥有,而是由调用<$请问自己应该由返回什么。

>

  $ obj = new Class();如果对象不能被构造, 

应分配给 $ obj if该对象不能正确构造? NULL



你有几个选择:


  1. 从构造函数中删除任何非构造代码,以便构造函数不会失败。放入在另一个方法中失败的任何代码。示例:

      $ obj = new Class(); 
    if($ obj-> initSomeStateThatMayFail()){
    //好,继续...
    } else {
    // handle failure somehow ...
    }
    unset($ obj); // calls __destruct()


  2. 如果构造函数失败,使用一个静态工厂方法测试它是否有效的实例化类,并返回 null 或适当的新实例。

      class Class {
    static function makeClass(){
    if(testIfValidToInstantiate()){
    return new self
    } else {
    return null;
    }
    }
    // ...
    }

    $ obj = Class :: makeClass();
    if($ obj!== null){
    // proceed ...
    }


  3. 如果代码没有意义,如果类不能被构造,也许你应该抛出一个异常。

      class Class {
    function __construct(){
    if(!testIfValidToInstantiate())
    throw new Exception(Unexpected condition);
    // ...
    }
    }

    try {
    $ obj = new Class();
    // ...
    } catch(Exception $ e){
    //做某事来处理这种情况...
    }



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:

  1. 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()
    

  2. 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...
    }
    

  3. 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...
    }
    

这篇关于__destruct在__construct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆