在函数内部杀死脚本是不是一种糟糕的做法


is it bad practice to kill a script inside of a function?

我指的是将die()函数用于调试之外的其他用途。这是一种"行之有效"的情况,但这是不是一种糟糕的做法?

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控制台脚本,用于从特定结构中删除站点

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

如果这是一个好的做法是另一回事。我想说,出于测试目的,这是可以的,但您应该在最终代码中避免它。原因是它使得代码难以维护。考虑其他人深入您的代码并试图理解其中的逻辑。实际上,人们必须浏览所有代码才能偶然发现这个细节。很可能没有,所以代码的行为看起来是坏的。

相反,试着做其中一个:

  • 抛出异常以离开当前范围。这样的异常很可能会被调用范围捕获并吞噬,但这是一种清晰且可预测的返回方式。显然,你应该记录这种行为。

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

  • 重新构造代码,这样就没有理由突然终止执行。

你还没有告诉我们你对die的意图。。因此,我们无法判断您是否使用了正确的工具。。。

die和它的同义词exit都退出了脚本。如果这是你想要的,那没关系。

我倾向于将exit用于正常操作,将die用于错误情况(您无法正确恢复的情况,例如:无法连接到数据库)。我还使用了die的包装器,所以如果需要,我可以记录这样的事件。虽然两个命令都做相同的事情,但意图不同,我想在代码中表达这种意图。

在您的示例中,我将使用exit

相关文章: