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


i've got several places in php application where, depending on functions have been called before, need display different error messages if goes wrong.

for instance, in example below if call startedit() function on 'file' directly , file locked, should return error message. if startedit() function called result of startedit() function in parent 'folder' should display different error message (see example below):

<?php     class folder{         public $files = array();          public function startedit(){             //when start editing folder, start editing files within             foreach($this->files $file){                 $file->startedit();             }         }     }      class file{         public $islocked = true;          public function startedit(){             try{                 //if file locked need throw exception                 if($this->islocked==1){                      //loop through stack trace see if called folder class.                     foreach(debug_backtrace() $frame){                         if($frame['class']=='folder'){                             throw new exception("cannot edit folder because 1 of files within locked");                         }                     }                      //if not called folder class throw different error                     throw new exception("this file locked");                 }             }catch(exception $e){                 exit("exception: ".$e->getmessage());             }         }     }      //create new folder     $folder = new folder();     //put files within     $file1 = new file();     $file2 = new file();     $folder->files = array($file1, $file2);      //start editing folder - should return message 'cannot edit folder because 1 of files within locked'     $folder->startedit();      //try editing 1 of files - should return message 'this file locked'     $file1->startedit();  ?> 

this simplified version of i'm trying - in application error message depend on function called 5 or 6 stack frames ago.

my question - valid way of doing things? there better way of achieving this?

thanks!

update

just clear, don't want display actual stack trace user, want use stack trace create more helpful messages user. 1 other way guess pass parameter each function tells called from?

has else tried doing similar things? on appreciated!

in end decided not use debug_backtrace(). because array returned huge (depending on number of function calls), thought might affect performance. in case of example above have implemented method called canstartedit() on 'file' objects , called in 'folder' object - way throw exception in folder object without having in file object , work out came before.


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -