在函数内杀死脚本是不好的做法吗? [英] is it bad practice to kill a script inside of a function?

查看:24
本文介绍了在函数内杀死脚本是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是将 die() 函数用于除调试之外的其他用途.这是一种效果很好"的情况,但这是不好的做法吗?

I'm referring to using the die() function for something else than debugging. This is a "well it works" situation, but is it bad practice?

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

/**
 * This Command will help Cjw create a new demo site to start off
 * with the Multisite bundle.
 *
 * Class CreateSiteCommand
 * @package Cjw\GeneratorBundle\Command
 */
class RemoveSiteCommand extends ContainerAwareCommand
{
    private $vendor; //eg "Cjw"
    private $fullBundleName; //eg "SiteCjwNetworkBundle"
    private $fullBundleNameWithVendor; //eg "CjwSiteCjwNetworkBundle"
    (more vars)

    /**
     * this function is used to configure the Symfony2 console commands
     */
    protected function configure()
    {
        $this
            ->setName('cjw:delete-site')
            ->setDescription('Delete Cjw Site')
            ->addOption('db_user', null, InputOption::VALUE_REQUIRED, 'If set, the database user will be shown on the instructions');
    }

    /**
     * This function executes the code after the command input has been validated.
     *
     * @param InputInterface $input gets the user input
     * @param OutputInterface $output shows a message on the command line
     * @return int|null|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // set optional db_username

        $dialog = $this->getHelper('dialog');
        $reusable = new \Reusable();
        $fs = new Filesystem();
        $cmd = new \ColorOutput();

        //push only vendors into the vendors array
        $vendors = $reusable->getFileList($reusable->getMainDirectory('src'),true);


        //select a vendor from the array
        $vendor = $dialog->select(
            $output,
            'Select your vendor [1]: ',
            $vendors,
            1
        );

        $bundles_in_vendor = $reusable->getFileList($reusable->getMainDirectory('src/'.$vendors[$vendor]),true);

        //push bundles that start with 'Site' into array
        $sites = array();

        foreach($bundles_in_vendor as $bundle)
        {
            if($reusable->startsWith($bundle,'Site'))
            {
                array_push($sites,$bundle);
            }
        }

        $site_to_delete = $dialog->select(
            $output,
            'Select site to remove: ',
            $sites,
            1
        );

        $bundle_deletion_path = $reusable->getMainDirectory('src/'.$vendors[$vendor]).'/'.$sites[$site_to_delete];

        $are_you_sure = array('yes','no');
        $confirmation = $dialog->select(
            $output,
            'Are you sure you want to delete: '.$sites[$site_to_delete],
            $are_you_sure,
            1
        );

        if($are_you_sure[$confirmation] == 'yes')
        {
            echo $cmd->color('yellow','attempting to remove bundle in: '.$bundle_deletion_path);
            $fs->remove($bundle_deletion_path);

            //returns demo
            $sitename = strtolower($sites[$site_to_delete]);
            $sitename = substr($sitename,0,-6);
            $sitename = substr($sitename,4);
            $this->setRawSiteNameInput($sitename);

            // returns acmedemo
            $symlinkname =  strtolower($vendors[$vendor].substr($sites[$site_to_delete],0,-6));
            $this->removeSymlinks($symlinkname,$this->getRawSiteNameInput());

            $this->createSetters($vendor,substr($sites[$site_to_delete],0,-6));

            $this->deleteEzPublishExtension();
            echo $this->getFullLegacyPath();

            echo $cmd->color('green','deletion process completed.');
        }
        else
        {
            echo "deletion canceled";
            die();
        }

        function_that_further_deletion_process();
    }

这是一个 symfony2 控制台脚本,用于从特定结构中删除站点

This is a symfony2 console script that removes a site from a certain structure

推荐答案

这是完全安全的,如果这是您的问题,因为 php 作为一种准解释语言在终止执行时不会留下任何痕迹或工件.

That is perfectly safe, if that is your question, since php as a quasi interpreted language does not leave any traces or artifacts when terminating the execution.

如果这是一个好的做法是另一回事.我会说它用于测试目的很好,但你应该在最终代码中避免它.原因是它使代码难以维护.考虑其他人深入研究您的代码并试图理解其中的逻辑.人们实际上必须遍历所有代码才能偶然发现这一细节.机会是一个没有,所以你的代码的行为似乎被破坏了.

If it is a good practice is another thing. I'd say it is fine for testing purposes, but you should avoid it in final code. Reason is that it makes code hard to maintain. Consider someone else diving into your code and trying to understand the logic. One would actually have to go through all the code to stumble over this detail. Chances is one does not, so the behavior of your code appears broken.

而是尝试执行以下操作之一:

Instead try to do one of these:

  • 抛出异常离开当前作用域.这样的异常很可能会被调用范围捕获并吞下,但它是一种清晰且可预测的返回方式.显然,您应该记录此类行为.

  • throw an exception to leave the current scope. Such an exception might well be caught and swallowed by the calling scope, but it is a clear and predictable way of returning. Obviously you should document such behavior.

返回一个明显超出正常"返回范围的值.因此,例如 nullfalse 而不是典型值.这会强制调用范围检查返回值,但无论如何这是一个很好的做法.

return a value clearly out of scope to what would "normally" be returned. So for example null or false instead of a typical value. This forces the calling scope to check the return value, but that is good practice anyway.

重构您的代码,以便没有理由突然终止执行.

restructure your code such that there is no reason to suddenly terminate the execution.

这篇关于在函数内杀死脚本是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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