문제

I'm trying to make an error logging class, I have some functions to set up various output methods like DB, file, return and screen. I want all errors to be stored into an array and when __destruct() is called, I want to stop the client from waiting for data and the log details about errors the user experienced. This way they don't have to report errors to me.

I have 2 modes, a simple GUI to test the functionality and the actual script generates responses in JSON, machine to machine. For GUI the final dump is fine but for JSON it destroys the response. So all error reporting is off and I have to handle any errors that would be dumped on screen myself, hence the $return in function flush_log($return) which make the function return a string if set to true.

After reporting flushing the errors I want to:unset($this->log_arrays)
or empty: $this->log_arrays=Array();, but it is out of scope - I understand why, my function uses a local copy - but how do I reset the array?

[EDIT]:

I tried:

  • $this->log_arrays = Array();
  • $this->log_arrays = null;
  • array popping:

    for ($i = 1; count($this->log_arrays); $i++)  
    {  
        array_pop($this->log_arrays);  
    }  
    

But I think none of it can work because within a class function you work with copies of variables, so they're basically out of scope.

[/EDIT]:

This is an already simplified class.. :

<?php
    class log_strings extends mysqli 
    {
        private $log_arrays = Array();

        public function __construct($output_to_file=false, $output_to_db=true, $fall_back_file=true, $arguments, $ip=null)
        {
            // Setup mysqli connection, file handle or report error if one or all have failed.
            // Also check wich outputs should be used and keep that info for later.
        }

        public function log($level, $string)
        {
            $log_arrays[] = Array('level' => $level, 'string' => $string);
        }

        public function __destruct()
        {
            $this->flush_log();
        }

        public function flush_log($return=false)
        {
            if (!isset($log_arrays) && count($log_arrays) == 0)
            {
                return true;
            }

            if ($return)
            {
                return $this->return_output();
            }
            else
            {
                $success = false;

                // if enabled, output to db
                if ($this->output_to_db) 
                {
                    $success = $success || $this->mysqli_output();
                }

                // if enabled or if db failed and fallback is enabled, output to file
                if ($this->output_to_file || ($this->fall_back_file && !$success)) 
                {
                    $success = $success || $this->file_output();
                }
                // if neither file or db succeeded, dump on screen
                if ($success = false)
                {
                    $this->screen_dump();
                }

                return true;
            }
            unset($this->log_arrays); // <= This is what it is all about!
        }

        private function screen_dump()
        {
            foreach($this->log_arrays as $array)
            {
                echo "<strong>{$array['level']}</strong>{$array['string']}<br/>\n";
            }
        }

        private function mysqli_output()
        {
            // Output to db functionally equal to $this->screen_dump()
        }

        private function file_output()
        {
            // Output to file functionally equal to $this->screen_dump()
        }

        private function return_output()
        {
            // Return output functionally equal to $this->screen_dump()
        }
    }
?>
도움이 되었습니까?

해결책

Resetting the array should work

$this->log_arrays = array();

Unset a class property is a very bad idea. Because it may be used in other methods or other classes using a potentionally getter of your class.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top