Yii2无法在控制器动作中注册关闭功能

时间:2015-09-25 12:46:39

标签: php error-handling yii2 execution-time

我有一个控制器动作如下:

public function actionTest()
{
    set_time_limit(1);
    register_shutdown_function(function()
    {
        var_dump('shutdown');
    });

    while(true){} //infinite loop
}

我仍然得到致命错误“超过1秒的最长执行时间”

如果我没有睡眠或超过执行时间,则调用关闭功能。 根据{{​​3}}和互联网上的几个例子,即使超出执行时间,也应该调用关闭。

我应该如何处理执行时错误?

1 个答案:

答案 0 :(得分:0)

我敢打赌PHP在运行你的关机功能之前会抛出一个致命的错误。类似于"致命错误:超出最大执行时间"。 Yii的error handler捕获所有PHP错误,显示错误消息并在PHP有机会运行您的函数之前死亡。这样你的功能永远不会被执行。

这个问题有几种解决方案。首先,您可以通过将此行添加到import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import static javax.swing.JOptionPane.showMessageDialog; public class NewJFrame extends javax.swing.JFrame { int selsalad=0;//count of the selected checkbox this cant go more than 2 String selectedsalad[]=new String[2]; // Variables declaration - do not modify private javax.swing.JPanel jPanel1; public NewJFrame() { initComponents(); Salad(); showMessageDialog(null, "uncomment promlemmethod to see the probelm after you try this one"); problemmehtod(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 561, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 470, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> public void problemmehtod(){ // this is the problem now i can select any much when i call this i commented this one for(Component c : jPanel1.getComponents()) { if(c instanceof JCheckBox) { JCheckBox temp=(JCheckBox) c; if(temp.getName().equals("a1")){ temp.setSelected(true); } if(temp.getName().equals("a2")){ temp.setSelected(true); } } } } public void Salad() { JCheckBox a[]=new JCheckBox[5]; // int count =0 ;//to count rows in result set try { for(int x=0;x<5;x++) { jPanel1.setLayout(new GridLayout()); a[x]=new JCheckBox("a"+String.valueOf(x)); a[x].setName("a"+String.valueOf(x)); jPanel1.add(a[x]); a[x].setVisible(true); a[x].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if( e.getSource() instanceof JCheckBox){ String s=((JCheckBox) e.getSource()).getName(); if(((JCheckBox) e.getSource()).isSelected()) { if(selsalad<2) { selectedsalad[selsalad]=((JCheckBox) e.getSource()).getName(); selsalad=selsalad+1; } else { ((JCheckBox) e.getSource()).setSelected(false); showMessageDialog(null, "you cant have more than 2 salads"); } } if(!((JCheckBox) e.getSource()).isSelected()) { if(selectedsalad[0].equals(s)|| selectedsalad[1].equals(s) ) { if(selsalad<2) { selectedsalad[selsalad]=""; } selsalad=selsalad-1; //showMessageDialog(null, selsalad); } } } } }); } } catch (Exception e) { showMessageDialog(null, e); } } // End of variables declaration } 来完全禁用Yii对PHP错误的处理。

index.php

其次,您可以尝试使用error_reportingdisplay_errors指令在php.ini中禁用错误​​消息。

第三,您可以使用try-catch块捕获PHP错误并捕获ErrorException类。只是抓住异常而不采取任何措施: