how to use php set_error_handler for undefined indexes in arrays
Get the solution ↓↓↓I want to make a single function for set_error_handler() to deal with undefined indexes in an array with a specific name. For instance, the array is called: $products. If I have
$products = array(1 => 'a', 2 => 'b' // etc...
and later execute call:
$a = $products[0];
I get an error. I want to handle the error only for the array with the name $products and no other. How can I make that?
Please don't give me alternatives to this method of solving the problem. I already had a discussion about that and it was decided that it really is better to use this method. I must also warn that this is a super simplification of the real thing. I have already tried doing some research and nothing helped.
Answer
Solution:
Setting up the error handler is easy enough - examples here - but filtering based on the code which actually triggered the error just isn't achievable without some very messy regex matching (or something similar) against the error message string.
You're probably going to have to look at some other form of solution if you really require this functionality. You could useisset()
to check index validity beforehand, or create an array-style class (e.g. usingArrayAccess
orArrayObject
from the SPL) with some index-checking logic built-in.
I know you didn't want different solutions, but I'd be interested to find out why/how you decided that the method you're proposing really is the best way?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function update() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.