PHP - is it a good idea to use the results of debug_backtrace to display different error messages?

StackOverflow https://stackoverflow.com/questions/16519170

  •  21-04-2022
  •  | 
  •  

Question

I've got several places in my PHP application where, depending on what functions have been called before, I need to display different error messages if something goes wrong.

For instance, in the example below if you call the startEdit() function on a 'file' directly and the file is locked, it should return a certain error message. But if the startEdit() function is called as a result of the startEdit() function in its parent 'folder' it should display a different error message (see example below):

<?php
    class folder{
        public $files = array();

        public function startEdit(){
            //when we start editing a folder, start editing all files within it
            foreach($this->files as $file){
                $file->startEdit();
            }
        }
    }

    class file{
        public $isLocked = true;

        public function startEdit(){
            try{
                //if the file is locked we need to throw an exception
                if($this->isLocked==1){

                    //loop through the stack trace to see if this was called from the folder class.
                    foreach(debug_backtrace() as $frame){
                        if($frame['class']=='folder'){
                            throw new Exception("Cannot edit this folder because one of the files within it are locked");
                        }
                    }

                    //if not called from the folder class then throw a different error
                    throw new Exception("This file is locked");
                }
            }catch(Exception $e){
                exit("Exception: ".$e->getMessage());
            }
        }
    }

    //create a new folder
    $folder = new folder();
    //put some files within it
    $file1 = new file();
    $file2 = new file();
    $folder->files = array($file1, $file2);

    //start editing the folder - should return the message 'Cannot edit this folder because one of the files within it are locked'
    $folder->startEdit();

    //try editing one of the files - should return the message 'This file is locked'
    $file1->startEdit();

?>

This is a very much simplified version of what I'm trying to do - in my application the error message could depend on a function that was called 5 or 6 stack frames ago.

My question is - is this a valid way of doing things? Is there some better way of achieving this?

Thanks!

UPDATE

Just to be clear, I don't want to display the actual stack trace to the user, I want to use the stack trace to create more helpful messages for the user. One other way to do it I guess would be to pass another parameter to each function which tells it where it was called from?

Has anyone else tried doing similar things? Any help on this would be much appreciated!

Was it helpful?

Solution

In the end I decided not to use debug_backtrace(). This is because the array returned by it could be huge (depending on the number of function calls), so I thought it might affect performance. In the case of the example above I would have implemented a method called canStartEdit() on the 'file' objects and called this in the 'folder' object - this way I could just throw an exception in the folder object without having to do it in the file object and then work out what came before.

OTHER TIPS

there is a $limit option as mentioned in the docs

debug_backtrace

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top